C # obtain the geographic location through IP address in network programming (Supplement)

Source: Internet
Author: User

I have previously written an article titled "C # Getting the local name, IP address, domain name, and physical location through network programming", which can be used to obtain the physical location of the Local Machine Based on the entered website address, the main code of this part is to dynamically obtain its data through the online third-party interface provided by youdao. REFERENCE The popping_dancer Blog Code. The main code is shown in:

////// Obtain the physical address based on the IP address /////////
 Public static string GetstringIpAddress (string strIP) // strIP is IP {string sURL = "http://www.youdao.com/smartresult-xml/search.s? Type = ip & q = "+ strIP +" "; string stringIpAddress =" "; // location: using (XmlReader read = XmlReader. create (sURL) // obtain the content of the xml file returned by youdao {while (read. read () // read the next byte from the stream {switch (Read. nodeType) {case XmlNodeType. text: // retrieve the Text content if (string. format ("{0}", read. value ). toString (). trim ()! = StrIP) {stringIpAddress = string. Format ("{0}", read. Value). ToString (). Trim () ;}break ;}} return stringIpAddress ;}

The obtained results are shown in. However, you may have deleted the URL and cannot use this interface to obtain the geographical location. I have provided a new method to help everyone and that friend.

Due to limited capabilities, the physical address is only obtained through access to a third-party interface, and it depends largely on the database data provided by the website. If you use access to a local static database to find the corresponding geographic location, not applicable either. the visit URL is: http://www.freegeoip.net/xml/

The source code and display result are as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; // Xml document namespace GetLocation {class Program {static void Main (string [] args) {string strIP = "74.125.31.104 "; // ip address string strURL = "http://www.freegeoip.net/xml/" + strIP; // URL string Location = ""; // physical Location // get the tag node set XmlDocument doc = new XmlD through GetElementsByTagName Ocument (); // Xml document doc. load (strURL); // Load strURL to specify the XML data XmlNodeList nodeLstCity = doc. getElementsByTagName ("City"); // obtain the tag Location = "obtain a single physical Location:" + nodeLstCity [0]. innerText + ""; Console. writeLine (Location); // match the first node XmlNode root = doc by SelectSingleNode. selectSingleNode ("Response"); if (root! = Null) {string CountryName = (root. selectSingleNode ("CountryName ")). innerText; string RegionName = (root. selectSingleNode ("RegionName ")). innerText; string City = (root. selectSingleNode ("City ")). innerText; Location = "Country name:" + CountryName + "\ n region name:" + RegionName + "\ n City name:" + City; Console. writeLine (Location);} Console. read ();}}}

This article mainly uses GetElementsByTagName and SelectSingleNode to obtain the Tag extracted from the Xml document and the token:

The following provides some excellent content for you to learn. It is also convenient for you to refer to it next time:
1. C # obtain the local name, IP address, domain name, and physical location through network programming (this is my own article. Learn from this article)
Http://blog.csdn.net/eastmount/article/details/9270221
2. Get user location by IP address (using C # To obtain the physical location is also the basis of this article)
Http://www.codeproject.com/Questions/686644/Get-user-location-by-IP-address
3. Obtain the physical address of the computer and the interface for obtaining the current geographical location through the IP address-the column of Rob street meal (Use PHP to obtain excellent articles)
Http://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)
Http://www.dotnetcurry.com/showarticle.aspx? ID = 325
5. get uesr location by ip address (we hope you can learn about it in Stack Overflow)
Http://stackoverflow.com/questions/4327629/get-user-location-by-ip-address
6. XmlDocument. Load (url) Problem (read the weather information article, same as my method)
Http://www.cnblogs.com/sharpfeng/archive/2011/03/02/1968666.html
Finally, I hope this article will be helpful to everyone and help the student solve the problem. If there are errors or deficiencies in this article, forgive me! Because only this method is used to obtain physical locations through a third-party interface, it is highly dependent on third-party data. If the website is abolished, it is very troublesome, the query results for different URLs are also different. I hope there are good methods to discuss and share.
(By: Eastmount five o'clock P.M. http://blog.csdn.net/eastmount)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.