简体   繁体   English

从Windows应用程序获取IP地址位置

[英]Get IP address location from windows application

I have seen many questions and answer on stackoverflow regarding how to fetch geolocation of an IP address in asp.net but.. 关于如何在asp.net中获取IP地址的地理位置,我在stackoverflow上看到了很多问题和答案,但是..

How can I fetch the IP address location in winforms ? 如何获取winforms中的IP地址位置?

I am working on the C# winform application and I need to show user ,its ip address and Its location . 我正在研究C#winform应用程序,我需要显示用户,它的IP地址它的位置 I am able to show user's Local,External IP address but I could not find any way to show Location. 我能够显示用户的本地,外部IP地址,但我找不到任何显示位置的方法。

Any body knows if I can do this with any WebRequest or any other solution ? 任何机构都知道我是否可以使用任何WebRequest或任何其他解决方案执行此操作?

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.(eg www.whatismyipaddress.com) 将IP地址发送到显示IP地址位置的站点。(例如www.whatismyipaddress.com)

  2. fetching its source code. 获取其源代码。

  3. parsing its code and use string operations to get the location. 解析其代码并使用string操作来获取位置。

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. 但我知道这不是好方法,好像网站被关闭或移动或源代码中的任何更改将使我的代码无用。

IpInfo is nice service for IP related things. IpInfo是与IP相关的很好的服务。 They also have a nice API . 他们也有一个很好的API

In the code below, I will make a web request to this service and it will return the IP info. 在下面的代码中,我将向此服务发出Web请求,它将返回IP信息。

This will return your IP info: 这将返回您的IP信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM