1

I'm using the WebBrowser control and when a user navigates to, for example, http://www.google.co.uk I'd like to display the IP address of the host that user is "connected" to.

Presently, I wait for the DocumentComplete event to fire, then use Dns.GetHostEntry("http://www.google.co.uk"); but this returns an AddressListwhich is an array of IP addresses, not the actual IP the user is connected to. In this example, 16 IPv4 addresses are returned.

How do I get the IP address the user is connected to and not all the available addresses?

Code snippet:

     private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
     {
            IPHostEntry Host = Dns.GetHostEntry("http://www.google.co.uk");
            foreach(var ip in Host.AddressList)
                 Debug.WriteLine("Host address list: " + ip);
     }
TEK
  • 1,265
  • 1
  • 15
  • 30
  • 1
    When I run the `Dns.GetHostEntry("www.google.co.uk")` (without the http) i only get a single IP (74.125.21.94), i.e. i'd remove the `http://` bit before querying the IP – rbm Sep 29 '15 at 15:08

1 Answers1

2

Remove the http and https before querying IP:

var sample_hosts = new[] {"http://www.google.co.uk", "https://www.google.co.uk", "http://www.amazon.com"};
foreach (var host in sample_hosts)
{
    var queryhost = host.Replace("http://", "").Replace("https://", "");
    var hostEntry = Dns.GetHostEntry(queryhost);
    foreach (var ip in hostEntry.AddressList)
    {
        Console.WriteLine("IP for {0}: {1}", queryhost, ip);
    }
}

prints

IP for www.google.co.uk: 64.233.176.9
IP for www.google.co.uk: 64.233.176.9
IP for www.amazon.com: 176.32.98.166

But since you're be using URL, then you also do something like

var url = "http://www.amazon.co.uk/My-Story-Steven-Gerrard/dp/1405923385/ref=zg_bs_books_5";
var uri = new Uri(url);
Console.WriteLine(uri.Host);

which returns the actual host:

www.amazon.co.uk
rbm
  • 3,243
  • 2
  • 17
  • 28
  • This sadly still returns 16 IP addresses for me (in the 62.253.72.[140 to 187) range). When I checked `www.google.co.uk` using a bunch of "IP finders" I could find on the Internet, the results are all different, too. Is there another way of finding the host IP the user has navigated to do you know? – TEK Sep 29 '15 at 15:29
  • @TEK consider clarifying your goal (maybe even in separate post - this one clearly tainted by you posting non-working code). Why would you need to know *exact* IP that was used to return original HTML page? – Alexei Levenkov Sep 29 '15 at 15:34
  • @AlexeiLevenkov The reason someone may want to know the *exact* IP is for legal reasons. I point my browser to visit an arbitrary URL, and somewhere in the ether I'm connected to a server with an IP address/es. Some hosts have many IP addresses, so just getting all the IP addresses that host has isn't much use if I may ever need to prove which server exactly (I know servers could have many public IP addresses, but surely there must be someway to obtain the IP address I was, so to speak, assigned when i first established that connection). Sometimes its nice to just be exact. – TEK Sep 29 '15 at 15:48
  • Maybe you could try something like [this](http://stackoverflow.com/questions/13806435/how-can-i-get-all-the-the-active-tcp-connections-using-net-framework-no-unmana) - try getting active TCP connections. – rbm Sep 29 '15 at 15:56