Get IP address location from windows application

只谈情不闲聊 提交于 2019-12-05 22:39:47

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!