Obtain the address of the corresponding location through the IP address

Source: Internet
Author: User

It has always been a dream to use an IP address to obtain the address of the corresponding location. I have worked hard so far, but the effect is not very good. Now I will paste the results of my efforts (some from the network, I hope to inspire some people. If some people have new insights, I hope I will not hesitate to give them some advice:

1. Use the Internet to obtain the IP address of the corresponding location:

Import java. io. inputStream; import java. io. outputStream; import java. io. outputStreamWriter; import java.net. URL; import java.net. URLConnection; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. w3c. dom. document; import org. w3c. dom. node; import org. w3c. dom. nodeList; public class IpReport {/*** parses the XML returned by the server ** @ return String, and splits */public static String getI P (String ip) {try {DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); dbf. setNamespaceAware (true); DocumentBuilder db = dbf. newDocumentBuilder (); InputStream is = getSoapInputStream (ip); Document doc = db. parse (is); NodeList nodeList = doc. getElementsByTagName ("string"); StringBuffer stringBuffer = new StringBuffer (); for (int count = 0; count <nodeList. getLength (); count ++) {Node n = no DeList. item (count); if (n. getFirstChild (). getNodeValue (). equals ("the query result is blank! ") {StringBuffer = new StringBuffer (" # "); break;} stringBuffer. append (n. getFirstChild (). getNodeValue () + "#"); // System. out. println (":" + n. getFirstChild (). getNodeValue ();} is. close (); return stringBuffer. toString (). split ("#") [1];} catch (Exception e) {e. printStackTrace (); return null ;}/ *** the user sends the SOAP request to the server, and return the input stream returned by the server point. The user enters the city name ** @ return the input stream returned by the server side for the client to read */private static InputStream getSoapInpu TStream (String ip) throws Exception {try {String soap = getSoapRequest (ip); if (soap = null) {return null ;} URL url = new URL ("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx"); URLConnection conn = url. openConnection (); conn. setUseCaches (false); conn. setDoInput (true); conn. setDoOutput (true); conn. setRequestProperty ("Content-Length", Integer. toString (soap. length (); conn. setRe QuestProperty ("Content-Type", "text/xml; charset = UTF-8"); conn. setRequestProperty ("SOAPAction", "http://WebXml.com.cn/getCountryCityByIp"); OutputStream OS = conn. getOutputStream (); OutputStreamWriter osw = new OutputStreamWriter (OS, "UTF-8"); osw. write (soap); osw. flush (); osw. close (); InputStream is = conn. getInputStream (); return is;} catch (Exception e) {e. printStackTrace (); return null ;}/ *** get SOAP request Header, and replace the flag with the user-entered city ** @ return the SOAP request that the customer will send to the server */private static String getSoapRequest (String ip) {StringBuilder sb = new StringBuilder (); sb. append ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+" <Soap: Envelope xmlns: xsi = \ "external" "+" xmlns: xsd = \ "http://www.w3.org/2001/XMLSchema\" "+" xmlns: soap = \ "http://schemas.xmlsoap.org/soap/envelope/\"> "+" <soap: body> "+" <getCountryCityByIp xmlns = \ "http://WebXml.com.cn/\"> "+" <theIpAddress> "+ ip +" </theIpAddress> "+" </getCountryCityByIp> "+ "</soap: body> </soap: Envelope> "); return sb. toString ();} public static void main (String [] args) throws Exception {System. out. println (getIp ("218.25.139.160"); // output: ADSL System of Dalian Unicom, Liaoning Province. out. println (getIp ("200.151.111.111"); // output result: Sao Paulo System, Brazil. out. println (getIp ("125.13.13.4"); // output result: ATHOME network System in Japan. out. println (getIp ("58.16.209.19"); // output result: System of tenglong Internet cafe in liuzhi Special Zone, liupanshui city, Guizhou Province. out. println (getIp ("22.8.129.60"); // output result: DoD Network Information Center }}

2. Use the qqwry. dat file to obtain the IP address of the corresponding location:

Because the qqwry. dat file provides a lot of code to get the IP address, only the address for downloading the java project is provided here:

Download Demo project]

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.