如何获取HttpWebRequest连接的服务器的IP地址?

dr.*_*vil 7 .net ip dns httpwebrequest

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

反正在.NET 3.5中有没有这样做?

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

小智 5

这是一个工作示例:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)


Dus*_*vis 4

干得好

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;
        }
Run Code Online (Sandbox Code Playgroud)

使用remoteEndPoint来收集你想要的数据。