DSN可以返回多个IP地址,因此在我希望获得我的HttpWebRequest连接到的IP之后,可以使用DNS解析来获取IP地址。

无论如何,.NET 3.5可以做到这一点吗?

例如,当我对www.microsoft.com进行简单的Web请求时,我想了解它连接了哪个IP地址以发送HTTP请求,因此我希望通过编程方式(而不是通过Wireshark等)实现此目的。

最佳答案

干得好

static void Main(string[] args)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
            req.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPoint1);

            Console.ReadKey();
        }

        public static IPEndPoint BindIPEndPoint1(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
        {
            string IP = remoteEndPoint.ToString();
            return remoteEndPoint;
        }

使用remoteEndPoint收集所需的数据。

10-04 19:20