Dan*_*dor 3 .net c# ip-address geolocation
我在NAT路由器后面的本地网络上有一台计算机.我有一些192.168.0.x地址,但我真的想知道我的公共 IP地址,而不是提到的
要么
我需要C#代码.
可能吗?如果是这样,怎么样?
Jim*_*hel 24
我更喜欢http://icanhazip.com.它返回一个简单的文本字符串.不需要HTML解析.
string myIp = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
Run Code Online (Sandbox Code Playgroud)
经过一些搜索,并通过扩展我的要求,我发现这不仅会让我获得IP,还会获得GEO位置:
class GeoIp
{
static public GeoIpData GetMy()
{
string url = "http://freegeoip.net/xml/";
WebClient wc = new WebClient();
wc.Proxy = null;
MemoryStream ms = new MemoryStream(wc.DownloadData(url));
XmlTextReader rdr = new XmlTextReader(url);
XmlDocument doc = new XmlDocument();
ms.Position = 0;
doc.Load(ms);
ms.Dispose();
GeoIpData retval = new GeoIpData();
foreach (XmlElement el in doc.ChildNodes[1].ChildNodes)
{
retval.KeyValue.Add(el.Name, el.InnerText);
}
return retval;
}
}
Run Code Online (Sandbox Code Playgroud)
返回XML,因此键/值字典将被填充:
<Response>
<Ip>93.139.127.187</Ip>
<CountryCode>HR</CountryCode>
<CountryName>Croatia</CountryName>
<RegionCode>16</RegionCode>
<RegionName>Varazdinska</RegionName>
<City>Varazdinske Toplice</City>
<ZipCode/>
<Latitude>46.2092</Latitude>
<Longitude>16.4192</Longitude>
<MetroCode/>
</Response>
Run Code Online (Sandbox Code Playgroud)
为方便起见,返回班级:
class GeoIpData
{
public GeoIpData()
{
KeyValue = new Dictionary<string, string>();
}
public Dictionary<string, string> KeyValue;
}
Run Code Online (Sandbox Code Playgroud)
问题是您要查找的IP地址不属于您的计算机.它属于您的NAT路由器.我能想到的唯一方法是使用外部服务器或者使用某种方式查询路由器.
如果您的路由器支持SNMP,您可以通过这种方式获得.
| 归档时间: |
|
| 查看次数: |
12571 次 |
| 最近记录: |