How to get IP address of server in .NET?
How do you get the IP address of the web/application server in .NET? Not the client IP address, but the server IP address.
I开发者_JAVA技巧 just found something about server variables.
You probably want something like the following code, to get all IP addresses of the current machine. However it won't tell you which network adaptor (and thus IP address) a particular request came in on, if you have more than one.
String strHostName = Dns.GetHostName();
Console.WriteLine("Host Name: " + strHostName);
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
Console.WriteLine(ipaddress.ToString());
}
To get my own IP address in C#
IPHostEntry ipEntry = DNS.GetHostByName (Dns.GetHostName());
IPAddress [] addr = ipEntry.AddressList;
To get for other's machine
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
There are services that tell you what your external IP address is, but it might be subject to change.
Sample Services:
- http://www.whatismyip.com/
- http://www.dnsstuff.com/
To get the IP address (and Coutnry/location) of a server programatically I use Utrace.de API. It returns an XML with IP address and location information too.
Example query: http://xml.utrace.de/?query=google.com
精彩评论