如何获取 HttpWebRequest 连接到的服务器的 IP 地址? [英] How to get IP address of the server that HttpWebRequest connected to?

查看:35
本文介绍了如何获取 HttpWebRequest 连接到的服务器的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DSN 可以返回多个 IP 地址,因此我想获取 HttpWebRequest 连接到的 IP,而不是使用 DNS 解析来获取我的请求后的 IP 地址.

DSN can return multiple IP addresses so rather then using DNS resolving to get the IP address after my request I want to get the IP that my HttpWebRequest connected to.

无论如何在 .NET 3.5 中这样做?

Is there anyway to do this in .NET 3.5?

例如,当我向 www.microsoft.com 发出一个简单的 Web 请求时,我想知道它连接了哪个 IP 地址来发送 HTTP 请求,我想以编程方式(而不是通过 Wireshark 等.em>)

For example when I do a simple web request to www.microsoft.com I want to learn that which IP address it connected to send the HTTP request, I want to this programmatically (not via Wireshark etc.)

推荐答案

这是一个工作示例:

using System;
using System.Net;

class Program
{
    public static void Main ()
    {
        IPEndPoint remoteEP = null;
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
        req.ServicePoint.BindIPEndPointDelegate = delegate (ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) {
            remoteEP = remoteEndPoint;
            return null;
        };
        req.GetResponse ();
        Console.WriteLine (remoteEP.Address.ToString());
    }
}

这篇关于如何获取 HttpWebRequest 连接到的服务器的 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆