[英]Smart way to get the public Internet IP address/geo loc
我在NAT路由器后面的局域網中有一台計算機。 我有一些192.168.0.x地址,但我真的想知道我的公共 IP地址,而不是在
要么
我需要C#代碼。
可能嗎? 如果是這樣,怎么辦?
我更喜歡http://icanhazip.com 。 它返回一個簡單的文本字符串。 無需HTML解析。
string myIp = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
經過一番搜索,並通過擴展需求,我發現這不僅可以獲取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;
}
}
返回的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>
為了方便起見,請返回課程:
class GeoIpData
{
public GeoIpData()
{
KeyValue = new Dictionary<string, string>();
}
public Dictionary<string, string> KeyValue;
}
問題是您要查找的IP地址不屬於您的計算機。 它屬於您的NAT路由器。 我能想到的唯一方法是使用外部服務器或查詢路由器的方法。
如果您的路由器支持SNMP,則可以通過這種方式獲取它。
我相信您確實需要連接一些服務器來獲取外部IP。
根據您使用的路由器,很可能直接從路由器上獲得它。 它們中的大多數都具有Web界面,因此只需導航到正確的網頁(例如“ 192.168.0.1/任何”),然后從該頁面“抓取”外部IP地址即可。 當然,這樣做的問題是它非常脆弱-如果您更改(甚至重新配置)路由器,則很有可能會損壞。
如果失敗,您也許可以使用uPNP並退回到whatsmyip.com。
如果您擔心連接丟失或站點的可用性,也可以嘗試使用這種方法來避免出現此問題,方法是包括上述建議。
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;
希望這對您有所幫助。
我這樣做是這樣的:我創建了一個保存地理位置數據的類
[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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.