How to get IP address of the server that HttpWebRequest connected to?

后端 未结 2 682
青春惊慌失措
青春惊慌失措 2020-12-19 05:23

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.

2条回答
  •  一个人的身影
    2020-12-19 05:51

    here you go

    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;
            }
    

    Use remoteEndPoint to collect the data you want.

提交回复
热议问题