Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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.

Is there anyway to do this in .NET 3.5?

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...