729
技術社區[雲棲]
C# 網絡編程之通過ip地址獲取地理位置(補充)
前麵我寫過一篇文章"C# 網絡編程之獲取本機名、ip地址、域名、物理位置"裏麵可以根據輸入的網址根據其ip地址獲取器物理位置,其中該部分主要代碼是通過有道網提供的在線第三方接口實現動態獲取它的數據.引用popping_dancer的博客代碼,其主要代碼如下圖所示:
/// <summary> /// 根據IP 獲取物理地址 /// </summary> /// <param name="strIP"></param> /// <returns></returns> public static string GetstringIpAddress(string strIP) //strIP為IP { string sURL = "https://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + strIP + ""; string stringIpAddress = ""; //地理位置 using (XmlReader read = XmlReader.Create(sURL)) //獲取youdao返回的xml格式文件內容 { while (read.Read()) //從流中讀取下一個字節 { switch (read.NodeType) { case XmlNodeType.Text: //取xml格式文件當中的文本內容 if (string.Format("{0}", read.Value).ToString().Trim() != strIP) { stringIpAddress=string.Format("{0}", read.Value).ToString().Trim(); } break; } } } return stringIpAddress; }
當時獲取的結果如下圖所示,但是可能由於有道已經刪除該URL網址,現在已經不能通過該接口獲取地理位置.所有我又提供了一種新的方法,希望能幫助到大家和那位朋友.
由於能力有限,隻會通過訪問第三方接口獲取物理地址,而且很大程度上取決於該網站提供的庫數據,如果采用訪問本地靜態的庫查找相應的地理位置,也不太適用.現在訪問的網址是:https://www.freegeoip.net/xml/
源代碼及顯示的結果如下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; //Xml文檔 namespace GetLocation { class Program { static void Main(string[] args) { string strIP = "74.125.31.104"; //ip地址 string strURL = "https://www.freegeoip.net/xml/" + strIP; //網址URL string Location = ""; //物理位置 //通過GetElementsByTagName獲取標簽結點集合 XmlDocument doc = new XmlDocument(); //Xml文檔 doc.Load(strURL); //加載strURL指定XML數據 XmlNodeList nodeLstCity = doc.GetElementsByTagName("City"); //獲取標簽 Location = "獲取單個物理位置:" + nodeLstCity[0].InnerText + ""; Console.WriteLine(Location); //通過SelectSingleNode匹配匹配第一個節點 XmlNode root = doc.SelectSingleNode("Response"); if (root != null) { string CountryName = (root.SelectSingleNode("CountryName")).InnerText; string RegionName = (root.SelectSingleNode("RegionName")).InnerText; string City = (root.SelectSingleNode("City")).InnerText; Location="國家名稱:"+CountryName+"\n區域名稱:"+RegionName+"\n城市名稱:"+City; Console.WriteLine(Location); } Console.Read(); } } }
文章主要通過GetElementsByTagName和SelectSingleNode兩種方法獲取Xml文檔中提取標簽Tag,獲取https://www.freegeoip.net/xml/www.baidu.com中的物理位置.推薦大家學習C#中讀取Xml文件製定結點的知識.運行結果如下圖所示:
下麵提供一些這方麵的有些非常優秀的內容僅供大家學習,同時也方便自己下次查閱:
1.C# 網絡編程之獲取本機名\IP地址\域名\物理位置 (這是我自己的文章,結合該篇文章學習)
https://blog.csdn.net/eastmount/article/details/9270221
2.Get user location by IP address(使用C#獲取物理位置 也是該篇文章的基礎)
https://www.codeproject.com/Questions/686644/Get-user-location-by-IP-address
3.獲取電腦物理地址以及通過IP地址獲取當前地理位置的接口-搶街飯的專欄(使用PHP獲取非常優秀的文章)
https://blog.csdn.net/lzwjavaphp/article/details/6972667
4.IP Address Location In ASP.NET(consume a free online API and fetch data using LINQ To XML)
https://www.dotnetcurry.com/showarticle.aspx?ID=325
5.get uesr location by ip address(關於它的討論 希望大家去Stack Overflow學習知識)
https://stackoverflow.com/questions/4327629/get-user-location-by-ip-address
6.XmlDocument.Load(url)問題(讀取天氣信息文章,與我的方法相同)
https://www.cnblogs.com/sharpfeng/archive/2011/03/02/1968666.html
最後希望該篇文章對大家有所幫助,同時希望該文章能幫助那位同學解決問題.如果文章中錯誤或不足之處,見諒!由於隻會采用這種通過第三方接口的方法獲取物理位置,很大程度依賴與第三方的數據,如果該網址被廢除,很麻煩,同時會遇到不同網址查詢結果也不同.希望有好的方法可以討論分享.
(By:Eastmount 2014-1-22 下午5點
https://blog.csdn.net/eastmount)
最後更新:2017-04-03 12:54:44