How to Get IP Address of Server responding to WebResponse C#
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 Bal开发者_运维技巧ancer, the IP address would then be the actual Web server which is what I am trying to find.
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.
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();
精彩评论