Question

Suppose I am retrieving a url as follows:

string url = "http://www.somesite.com/somepage.html"
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Is there a way I can see the IP address of the destination url? Does it require a separate call?

Thanks

Was it helpful?

Solution

Look at the System.Net.Dns class. You can get a list of IP addresses of the host from the Dns.GetHostEntry() method.

OTHER TIPS

Although Dns.GetHostEntry() can get you the IP of the server, in case there are multiple A records for the same host, you'll get them all (also known as round-robin DNS). However, when actually connecting to a web server, client picks one of those IPs.

There doesn't seem to be an exposed way to find out exactly which IP was used when connecting. I've found this information while working on our web monitoring solution at www.justwentdown.com. This info is located in a private field of the web response,

myHttpWebResponse.ResponseStream.Connection.ServerAddress

However, because Connection and ServerAddress are private/internal properties, you'll need to use reflection to get the values. I've found this solution to be very useful in those situations.

I've tested it with .NET 4.0. It's a bit messy and may break with future versions of .NET, so I'd recommend adding a unit test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top