Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 674
dr. evil Avatar asked Jul 11 '11 19:07

dr. evil


People also ask

How can I get all IP addresses from a server?

Following are the 3 ways you can check website IP address:Check your Welcome Mail: IP address of the server is typically mentioned in the welcome email by the company. Use Ping Command: You can ping the webserver with the CLI, and find the webserver. Global DNS Checker for IP Lookup: Use Global DNS checker tool online ...

How do I find the IP address of a home server?

You can quickly search for the IP through the command prompt in Windows. Type ipconfig in the command prompt and press Enter to retrieve the address. The same command prompt is also useful for retrieving the IP address of another computer on the same network.

What is the IP address of server?

An IP address is a unique address that identifies a device on the internet or a local network. IP stands for "Internet Protocol," which is the set of rules governing the format of data sent via the internet or local network.


2 Answers

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());
    }
}
like image 54
moander Avatar answered Oct 21 '22 13:10

moander


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.

like image 23
Dustin Davis Avatar answered Oct 21 '22 14:10

Dustin Davis