Get IP address location from windows application

I saw a lot of questions and answered stackoverflow regarding how to get geolocation of an IP address in asp.net , but ..

How can I find the IP address location in winforms ?

I am working on a C # winform application and I need to show the user, his IP address and his location . I can show the local, external IP address of the user, but I could not find a way to show the location.

Does any body know if I can do this with any WebRequest or any other solution?

Edit: I can complete the task by following the method.

  • Send an IP address to a site that shows the location with an IP address (e.g. www.whatismyipaddress.com)

  • source code extraction.

  • parsing code and using string operations to get location.

But I know that this is not a good approach, as the website is not working or moving, or any change in the source code will make my code useless.

+3
source share
3 answers

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

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

This will return your IP address information:

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

Example:

 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"}

+1
source
+4
source

Enjoy

 For querying GeoIP information about your own IP: http://ip-json.rhcloud.com/json http://ip-json.rhcloud.com/xml http://ip-json.rhcloud.com/csv For querying GeoIP information about IP address: http://ip-json.rhcloud.com/json/64.27.57.24 http://ip-json.rhcloud.com/xml/64.27.57.24 http://ip-json.rhcloud.com/csv/64.27.57.24 For querying GeoIP information about a domain: http://ip-json.rhcloud.com/json/www.google.com http://ip-json.rhcloud.com/xml/www.google.com http://ip-json.rhcloud.com/csv/www.google.com You can use curl command in terminal: curl ip-json.rhcloud.com curl ip-json.rhcloud.com/ua curl ip-json.rhcloud.com/all curl ip-json.rhcloud.com/json curl ip-json.rhcloud.com/xml 
0
source

Source: https://habr.com/ru/post/1482408/


All Articles