如何获取响应 WebResponse C# 的服务器的 IP 地址
我试图在 C# 中找到响应我的 WebRequest 的 IP 地址(不是主机名)。我不想进行 DNS 解析,因为在这种情况下,返回的 DNS 记录不是响应请求的服务器。例如:
客户 ->负载均衡器-> Web 服务器
DNS 服务器将使用负载均衡器的 IP 进行响应。假设响应的 Web 服务器不通过负载均衡器返回,则 IP 地址将是我正在尝试查找的实际 Web 服务器。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有权访问服务器端代码吗?或者到网络服务器配置?您始终可以将机器 IP 或任何您想要的标识符放在自定义标头中,然后在客户端上查找。
至于您原来的问题,我不认为 HttpWebRequest/HttpWebResponse 类会在任何地方公开信息。
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.
我认为您必须深入了解 OSI,并创建并利用您自己的套接字;
那么您将可以访问 RemoteEndPoint 属性(至少在您的套接字已连接或已连接到之后),如下所示:
Socket sprocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);< br>
sprocket.Connect("www.google.com", 80);
字符串 IPAddressOfRespondingServer = ((IPEndPoint)sprocket.RemoteEndPoint).Address.ToString();
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();