3

I have seen many questions and answer on stackoverflow regarding how to fetch geolocation of an IP address in asp.net but..

How can I fetch the IP address location in winforms ?

I am working on the C# winform application and I need to show user ,its ip address and Its location. I am able to show user's Local,External IP address but I could not find any way to show Location.

Any body knows if I can do this with any WebRequest or any other solution ?

Edit: I am able to accomplish the task by following method.

  1. Send IP address to a site which shows location from the IP address.(e.g. www.whatismyipaddress.com)

  2. fetching its source code.

  3. parsing its code and use string operations to get the location.

But I know It is not good approach as if website is down or moved or any change in the source code will make my code useless.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116

2 Answers2

4

You can use IP address geolocation database

TOUDIdel
  • 1,322
  • 14
  • 22
  • Depends how much info you need. THe cost is not that high, though - well worth paying. – TomTom Apr 11 '11 at 07:42
  • 1
    Yes IPInfoDB.com offers free services and they have just introduced their new Lite Version database, you will need to request for API key before you can download anything from there. –  Apr 11 '11 at 07:45
  • day b4 yesterday there were 3 answers for the same question and today u all can see only one answer ..do we have new feature on stackoverflow to remove answers ??? Im confused :O – Sangram Nandkhile Apr 12 '11 at 08:17
1

IpInfo is nice service for IP related things. They also have a nice API.

In the code below, I will make a web request to this service and it will return the IP info.

This will return your IP info:

public static string GetLocation(string ip)
{
    var res = "";
    WebRequest request = WebRequest.Create("http://ipinfo.io/" + ip);
    using (WebResponse response = request.GetResponse())
    using (StreamReader stream = new StreamReader(response.GetResponseStream()))
    {
        string line;
        while ((line = stream.ReadLine()) != null)
        {
            res += line;
        }
    }
    return res;
}

An example using this is:

Console.WriteLine (GetLocation("8.8.8.8"));

This will output:

{ "ip": "8.8.8.8", "hostname": "No Hostname", "city": "Mountain View", "region": "California", "country": "US", "loc": "37.3860,-122.0838", "org": "AS15169 Google Inc.", "postal": "94035"}

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474