tags:

views:

266

answers:

3

I am trying to find the ip address (not the hostname) that responded to my WebRequest in C#. I do not want to do a DNS resolution, because their are cases where the DNS records returned are not the servers responding to the request. For ex:

Client -> Load Balancer -> Web Server

The DNS server would respond with the Load Balancer's IP. Assuming the responding Web server is not going back through the Load Balancer, the IP address would then be the actual Web server which is what I am trying to find.

+1  A: 

Do you have access to the server side code? Or to the web server configuration? You could always place the machines IP, or whatever identifier you'd like, in a custom header and look for that on the client.

As for your original question, I do not believe that information is exposed anywhere by the HttpWebRequest/HttpWebResponse classes.

Michael Morton
No unfortunately I don't, otherwise it would be a great option.
Traderde
+1  A: 

I think you'll have to go OSI-dipping, and create and harness your own socket;
then you'll have access to the RemoteEndPoint property (at least after your socket has connected, or been connected to) like so :

Socket sprocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sprocket.Connect("www.google.com", 80);
string IPAddressOfRespondingServer = ((IPEndPoint)sprocket.RemoteEndPoint).Address.ToString();

Morten Bergfall
That would be the IP address at the other end of the connection, not necessarily an IP address of the response to a request - they could be different with proxy servers or load balancers involved.
John Saunders
I guess you're right....hmmm....I think I'll just side with your, mrSaunders that is, comment to the question; why indeed? Would be interested to see how it's done though. Is there a bulletproof way of obtaining the IP through the means discussed, that doesn't require "tweaking" either the packets or whatnot on both ends of the connection?
Morten Bergfall
A: 

Thanks Morten. That was what I was looking for.

traderde