获得从Windows应用程序的IP地址位置 [英] Get IP address location from windows application
问题描述
我见过很多的问题和回答关于计算器关于如何获取在asp.net中的IP地址的地理位置但..
I have seen many questions and answer on stackoverflow regarding how to fetch geolocation of an IP address in asp.net but..
我如何可以获取在的WinForms
我工作的 C#的winform的IP地址位置应用程序,我需要证明的用户,它的 IP地址和它的位置即可。
我能够显示用户的本地,外部IP地址,但我找不到任何方式来展示位置。
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.
任何机构都知道,如果我能做到这一点与任何的WebRequest
或任何其他解决方案
Any body knows if I can do this with any WebRequest
or any other solution ?
编辑:我能够完成通过下面的方法任务。
I am able to accomplish the task by following method.
-
发送IP地址,一个网站,显示来自该IP地址的位置。(如:www.whatismyipaddress。 COM)
Send IP address to a site which shows location from the IP address.(e.g. www.whatismyipaddress.com)
获取它的源代码。
解析它的代码,并使用字符串
操作来获取位置。
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.
推荐答案
IpInfo 是对与知识产权相关的美好的东西服务。他们也有一个很好的API 。
IpInfo is nice service for IP related things. They also have a nice API.
在代码。下面,我会做一个网络请求,这个服务,它会返回IP信息
In the code below, I will make a web request to this service and it will return the IP info.
这将返回您的IP信息:
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"));
这将输出:
{IP:8.8.8.8,主机名:无主机名,城市:山景,区:加州,国家:美国,禄:37.3860,-122.0838,组织:AS15169谷歌公司,邮政:94035}
这篇关于获得从Windows应用程序的IP地址位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!