When getting the url via HttpWebRequest, can I see the IP of the target server?

Suppose I am fetching the url like this:

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

      

Is there a way to see the IP of the target url? Is a separate call required?

thank

+2


source to share


2 answers


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



+1


source


Although it Dns.GetHostEntry()

can get the IP address of the server, in case there are multiple A records for the same host, you will get them all (also called round robin DNS servers). However, when actually connecting to the web server, the client chooses one of these IP addresses.

There seems to be no open way to find out which IP was used when connecting. I found this information while working on our www.justwentdown.com web monitoring solution . This information is in a private field of the web response,

myHttpWebResponse.ResponseStream.Connection.ServerAddress

      



However, since Connection

both ServerAddress

are private / intrinsic properties, you need to use reflection to get the values. I found this solution to be very helpful in these situations.

I tested it with .NET 4.0. This is a bit messy and might break with future versions of .NET, so I would recommend adding a unit test.

+6


source







All Articles