Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取公共互联网IP地址/geo loc的智能方式_C#_.net_Geolocation_Ip Address - Fatal编程技术网

C# 获取公共互联网IP地址/geo loc的智能方式

C# 获取公共互联网IP地址/geo loc的智能方式,c#,.net,geolocation,ip-address,C#,.net,Geolocation,Ip Address,我在本地网络上有一台计算机,在NAT路由器后面。我有一些192.168.0.x地址,但我真的想知道我的公共IP地址,而不是 或 我需要C代码 可能吗?如果是这样,怎么做?我认为您确实需要连接某个服务器以获取外部IP。问题是您要查找的IP地址不属于您的计算机。它属于你的NAT路由器。我能想到的唯一方法就是使用外部服务器或通过某种方式查询路由器 如果您的路由器支持SNMP,您可以通过这种方式获得它。根据您使用的路由器的不同,您可以直接从路由器获得它。它们中的大多数都有一个web界面,因此需要导航

我在本地网络上有一台计算机,在NAT路由器后面。我有一些192.168.0.x地址,但我真的想知道我的公共IP地址,而不是

我需要C代码


可能吗?如果是这样,怎么做?

我认为您确实需要连接某个服务器以获取外部IP。

问题是您要查找的IP地址不属于您的计算机。它属于你的NAT路由器。我能想到的唯一方法就是使用外部服务器或通过某种方式查询路由器


如果您的路由器支持SNMP,您可以通过这种方式获得它。

根据您使用的路由器的不同,您可以直接从路由器获得它。它们中的大多数都有一个web界面,因此需要导航到正确的web页面(例如,“192.168.0.1/随便什么”)并从该页面“抓取”外部IP地址。当然,问题是它非常脆弱——如果你改变(甚至重新配置)你的路由器,它很有可能会坏掉。

我更喜欢。它返回一个简单的文本字符串。不需要HTML解析

string myIp = new WebClient().DownloadString(@"http://icanhazip.com").Trim();

如果失败的话,您可以使用uPNP并返回whatsmyip.com。

经过一些搜索,通过扩展我的要求,我发现这不仅可以获得IP,还可以获得地理位置:

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

93.139.127.187
人力资源
克罗地亚
16
瓦拉兹丁斯卡
瓦拉兹丁斯基头虱
46.2092
16.4192
为方便起见,返回舱:

class GeoIpData
{
    public GeoIpData()
    {
        KeyValue = new Dictionary<string, string>();
    }
    public Dictionary<string, string> KeyValue;
}
classgeoipdata
{
公共地理数据()
{
KeyValue=新字典();
}
公共字典键值;
}

如果您担心连接丢失或站点的可用性,您也可以尝试通过包含上述建议来避免该问题

    using System.Threading;

    Task<string>[] tasks = new[]
    {
      Task<string>.Factory.StartNew( () => new System.Net.WebClient().DownloadString(@"http://icanhazip.com").Trim() ),
      Task<string>.Factory.StartNew( () => new System.Net.WebClient().DownloadString(@"http://checkip.dyndns.org").Trim() )
    };

    int index = Task.WaitAny( tasks );
    string ip = tasks[index].Result;
使用系统线程;
任务[]任务=新任务[]
{
Task.Factory.StartNew(()=>new System.Net.WebClient().DownloadString(@)http://icanhazip.com“).Trim()),
Task.Factory.StartNew(()=>new System.Net.WebClient().DownloadString(@)http://checkip.dyndns.org“).Trim())
};
int index=Task.WaitAny(任务);
字符串ip=任务[索引]。结果;
希望这个也能有所帮助。

我是这样做的: 我创建了一个保存地理位置数据的类

[Serializable]
public class LocationData
{
     public string IP { get; set; }
     public string CountryCode { get; set; }
     public string CountryName { get; set; }
     public string RegionCode { get; set; }
     public string City { get; set; }
     public string ZipCode { get; set; }
     public string TimeZone { get; set; }
     public string Latitude { get; set; }
     public string Longitude { get; set; }
     public string MetroCode { get; set; }
}
然后,我使用以下代码调用地理数据并填充类

public static LocationData GetLocation(string ip= "")
{
    using (var client = new System.Net.WebClient())
    {
         XmlRootAttribute xRoot = new XmlRootAttribute();
         xRoot.ElementName = "Response";
         string downloadedString = client.DownloadString("http://freegeoip.net/xml/" + ip);
           XmlSerializer mySerializer = new XmlSerializer(typeof(LocationData), xRoot) ;
        using (XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(downloadedString)))
        {
             return mySerializer.Deserialize(xmlReader)as LocationData;
        }
     }
}
因为答案是“坏”xml,所以需要指定xRoot元素,否则会出现错误

快乐编码


下面的代码将帮助您获取公共IP地址

    string _externalIP;
    _externalIP = (new WebClient()).DownloadString("http://http://icanhazip.com/");
    _externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                 .Matches(externalIP)[0].ToString();

在不依赖公共服务器的情况下获取外部IP?为什么?您是否意识到您的“外部”IP在某种程度上始终依赖于公共互联网?可能是最简单的“作弊”方式:使用
WebClient打开并解析输出以获取IP地址我取消了不使用外部服务器停止对我进行攻击的要求…whatismyip.com有一个脚本和程序的自动化页面,可供使用,该页面只返回IP地址,因此您无需进行屏幕清理。URL是从这里链接的:uPNP听起来很有希望,但是用什么库呢?c#?您如何在代码隐藏中使用此功能?请您详细描述一下。