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

后端 未结 2 686
青春惊慌失措
青春惊慌失措 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:53

    This is a working example:

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

提交回复
热议问题