views:

671

answers:

3

Anyone know how to do this? Can it be done?

A: 

Why use Web browser control (Dont know if it can be done) when you can get IP address using

IPHostEntry ip = default(IPHostEntry);
ip = Dns.GetHostByName(Dns.GetHostName);
Shoban
A: 

As Shoban suggests, if you're looking for the IP of the local machine, there's no need to use the browser control. I'd prefer the below to get the IP, though; GetHostByName is marked as obsolete.

IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in entry.AddressList)
{
    Console.WriteLine(ip);
}

You can and often will see multiple IPs for the same host.

If you're looking for the IP of the machine you've navigated to in a WebBrowser control, grab the URL from the Url property of the control, and do the same thing:

Uri url = new Uri(browser.Url);
IPHostEntry entry = Dns.GetHostEntry(url.Host);
foreach (IPAddress ip in entry.AddressList)
{
    Console.WriteLine(ip);
}

EDIT: Proximo indicates that he's looking for his computer's external IP address. The site WhatIsMyIp (thanks to Brian for the reference) presents the IP as the only returned content; nothing simpler to scrape:

WebClient wc = new WebClient();
string ip = wc.DownloadString("http://www.whatismyip.org/");
Console.WriteLine(ip);

Be kind to the site, and respect any usage restrictions they impose.

Michael Petrotta
http://whatismyip.com/automation/n09230945.asp works too (http://whatismyip.com/automation/ for information). Their usage restrictions are once per 300 seconds. I don't know what the restrictions are for whatismyip.org, but expect your request to time out if you exceed them.
Brian
A: 

Hey, This is me actually. I try your method and it works but I am trying to get the IP address of a proxy that I am using. This method doesn't show the proxy IP address.

Proximo
Describe what you tried, the results you got, and the results you expected. Better yet, expand your question with that information.
Michael Petrotta
I used your codeUri url = new Uri(browser.Url);IPHostEntry entry = Dns.GetHostEntry(url.Host);foreach (IPAddress ip in entry.AddressList){ Console.WriteLine(ip);}Results//{fe80::20e0:dbf:c09e:6178%12}//{fe80::51cd:3940:fa3c:bafb%11}//{2002:3f61:9e87::3f61:9e87}//{63.97.158.135} <-- My actual IP//{2001:0:4137:9e50:20e0:dbf:c09e:6178}But I don't want my actually IP but the Proxy IP that IE is using. I'm not sure if that's possible, but when I browse to www.whatismyip.com it shows my proxy IP in the webbrowser control windows.
Proximo
Ah. There's no easy way to do what you're asking for. See this thread for some background: http://stackoverflow.com/questions/917332/getting-my-ip-address. Two options they present: the STUN protocol, and simply scraping the html returned by www.whatsmyip.com.
Michael Petrotta
#Michael: scraping from www.whatismyip.org is easier.
Brian
@Brian - nothing could be easier, unless they delivered the IP to me in the mails, written on parchment. Expanded my answer with that.
Michael Petrotta