java method of obtaining detailed geographic information based on the ip address

  • 2020-06-07 04:26:03
  • OfStack

There are many interfaces on the Internet that can query the specific location through ip, as follows:

Get the IP location through Taobao IP address library

Request interface (GET) : http: / / ip taobao. com/service/getIpInfo php & # 63; ip=[ip address string]

Response information :(json format) country, province (autonomous region or municipality), city (county), operator

Return data format:


{ " code " :0, " data " :{ " ip " : " 210.75.225.254 " , " country " : " \u4e2d\u56fd " , " area " : " \u534e\u5317 " , 
 " region " : " \u5317\u4eac\u5e02 " , " city " : " \u5317\u4eac\u5e02 " , " county " : "" , " isp " : " \u7535\u4fe1 " , 
 " country_id " : " 86 " , " area_id " : " 100000 " , " region_id " : " 110000 " , " city_id " : " 110000 " , 
 " county_id " : " -1 " , " isp_id " : " 100017 " }} 

Where the value of code means 0: success, 1: failure.

Sina interface: http: / / int dpool. sina. com. cn/iplookup/iplookup php & # 63; format = json & ip=218.192.3.42

The return value


var remote_ip_info = { " ret " :1, " start " : " 218.192.0.0 " , " end " : " 218.192.7.255 " , " country " : " \u4e2d\u56fd " , " province " : " \u5e7f\u4e1c " , " city " : " \u5e7f\u5dde " , " district " : "" , " isp " : " \u6559\u80b2\u7f51 " , " type " : " \u5b66\u6821 " , " desc " : " \u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662 " };

通过jqry HuoQuXiangYingDeShuJu


$.getScript( Data interface ',function(){ 
// Sina: remote_ip_info.country 
})

Share project address for IP location: tencent IP http: / / ip qq. com/cgi - bin/searchip & # 63; searchip1 = ip

Using Java to call taobao ip query interface to query 1 instance of java in the region:


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 *  According to the IP Address gets detailed geographic information 
 * @project:personGocheck
 * @class:AddressUtils.java
 * @author:heguanhua E-mail:37809893@qq.com
 * @date : Nov 14, 2012 6:38:25 PM
 */
public class AddressUtils { 
 /**
 *
 * @param content
 *    Requested parameters   Format for: name=xxx&pwd=xxx
 * @param encoding
 *    The server side requests the encoding. Such as GBK,UTF-8 Etc. 
 * @return
 * @throws UnsupportedEncodingException
 */
 public String getAddresses(String content, String encodingString)
 throws UnsupportedEncodingException {
 //  This call pconline The interface of the 
 String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
 //  from http://whois.pconline.com.cn achieve IP Information about the province 
 String returnStr = this.getResult(urlStr, content, encodingString);
 if (returnStr != null) {
 //  Processing of returned provincial and municipal information 
 System.out.println(returnStr);
 String[] temp = returnStr.split(",");
 if(temp.length<3){
 return "0";// invalid IP , LAN testing 
 }
 String region = (temp[5].split(":"))[1].replaceAll("\"", "");
 region = decodeUnicode(region);//  provinces 

   String country = "";
   String area = "";
   // String region = "";
   String city = "";
   String county = "";
   String isp = "";
   for (int i = 0; i < temp.length; i++) {
    switch (i) {
    case 1:
     country = (temp[i].split(":"))[2].replaceAll("\"", "");
     country = decodeUnicode(country);//  countries 
     break;
     case 3:
      area = (temp[i].split(":"))[1].replaceAll("\"", "");
      area = decodeUnicode(area);//  region  
     break;
     case 5:
      region = (temp[i].split(":"))[1].replaceAll("\"", "");
      region = decodeUnicode(region);//  provinces  
     break; 
     case 7:
      city = (temp[i].split(":"))[1].replaceAll("\"", "");
      city = decodeUnicode(city);//  The city 
     break; 
     case 9:
       county = (temp[i].split(":"))[1].replaceAll("\"", "");
       county = decodeUnicode(county);//  region  
     break;
     case 11:
      isp = (temp[i].split(":"))[1].replaceAll("\"", "");
      isp = decodeUnicode(isp); // ISP The company 
     break;
    }
   }

 System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
 return region;
 }
 return null;
 }
 /**
 * @param urlStr
 *    Requested address 
 * @param content
 *    Requested parameters   Format for: name=xxx&pwd=xxx
 * @param encoding
 *    The server side requests the encoding. Such as GBK,UTF-8 Etc. 
 * @return
 */
 private String getResult(String urlStr, String content, String encoding) {
 URL url = null;
 HttpURLConnection connection = null;
 try {
 url = new URL(urlStr);
 connection = (HttpURLConnection) url.openConnection();//  New connection instance 
 connection.setConnectTimeout(2000);//  Sets the connection timeout in milliseconds 
 connection.setReadTimeout(2000);//  Sets the read data timeout in milliseconds 
 connection.setDoOutput(true);//  Whether to open the output stream  true|false
 connection.setDoInput(true);//  Whether to open the input stream true|false
 connection.setRequestMethod("POST");//  Submission methods POST|GET
 connection.setUseCaches(false);//  Whether the cache true|false
 connection.connect();//  Open connection port 
 DataOutputStream out = new DataOutputStream(connection
  .getOutputStream());//  Open the output stream to write data to the opposite server 
 out.writeBytes(content);//  Write the data , Submit your form  name=xxx&pwd=xxx
 out.flush();//  The refresh 
 out.close();//  Close the output stream 
 BufferedReader reader = new BufferedReader(new InputStreamReader(
  connection.getInputStream(), encoding));//  Writes data to the opposite end and returns data to the opposite server 
 // , In order to BufferedReader Flow to read 
 StringBuffer buffer = new StringBuffer();
 String line = "";
 while ((line = reader.readLine()) != null) {
 buffer.append(line);
 }
 reader.close();
 return buffer.toString();
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (connection != null) {
 connection.disconnect();//  Close the connection 
 }
 }
 return null;
 }
 /**
 * unicode  Converted to   Chinese 
 *
 * @author fanhui 2007-3-15
 * @param theString
 * @return
 */
 public static String decodeUnicode(String theString) {
 char aChar;
 int len = theString.length();
 StringBuffer outBuffer = new StringBuffer(len);
 for (int x = 0; x < len;) {
 aChar = theString.charAt(x++);
 if (aChar == '\\') {
 aChar = theString.charAt(x++);
 if (aChar == 'u') {
  int value = 0;
  for (int i = 0; i < 4; i++) {
  aChar = theString.charAt(x++);
  switch (aChar) {
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
  value = (value << 4) + aChar - '0';
  break;
  case 'a':
  case 'b':
  case 'c':
  case 'd':
  case 'e':
  case 'f':
  value = (value << 4) + 10 + aChar - 'a';
  break;
  case 'A':
  case 'B':
  case 'C':
  case 'D':
  case 'E':
  case 'F':
  value = (value << 4) + 10 + aChar - 'A';
  break;
  default:
  throw new IllegalArgumentException(
   "Malformed  encoding.");
  }
  }
  outBuffer.append((char) value);
 } else {
  if (aChar == 't') {
  aChar = '\t';
  } else if (aChar == 'r') {
  aChar = '\r';
  } else if (aChar == 'n') {
  aChar = '\n';
  } else if (aChar == 'f') {
  aChar = '\f';
  }
  outBuffer.append(aChar);
 }
 } else {
 outBuffer.append(aChar);
 }
 }
 return outBuffer.toString();
 }
 //  test 
 public static void main(String[] args) {
 AddressUtils addressUtils = new AddressUtils();
 //  test ip 219.136.134.157  China = South China = Guangdong province, = guangzhou = Yuexiu district = telecom 
 String ip = "125.70.11.136";
 String address = "";
 try {
 address = addressUtils.getAddresses("ip="+ip, "utf-8");
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 System.out.println(address);
 //  The output is: Guangdong Province , guangzhou , Yuexiu district 
 }
} 


Related articles: