当前位置: 移动技术网 > IT编程>开发语言>Java > java根据ip地址获取详细地域信息的方法

java根据ip地址获取详细地域信息的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

万宇豪个人资料,中原雄狮诞生记,贵烟价格表图

互联网有很多接口可以实现通过ip查询到具体的位置,如下:

通过淘宝ip地址库获取ip位置

请求接口(get):http://ip.taobao.com/service/getipinfo.php?ip=[ip地址字串]

响应信息:(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商

返回数据格式:

{“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”}} 

其中code的值的含义为,0:成功,1:失败。

新浪的接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42

返回值

复制代码 代码如下:

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 获取相应的数据

$.getscript(‘数据接口',function(){ 
//新浪:remote_ip_info.country 
})

腾讯ip分享计划的地址获取ip所在地: http://ip.qq.com/cgi-bin/searchip?searchip1=ip

用java调用淘宝ip查询接口查询地域的一个java实例:

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;
/**
 * 根据ip地址获取详细的地域信息
 * @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
 *   请求的参数 格式为:name=xxx&pwd=xxx
 * @param encoding
 *   服务器端请求编码。如gbk,utf-8等
 * @return
 * @throws unsupportedencodingexception
 */
 public string getaddresses(string content, string encodingstring)
 throws unsupportedencodingexception {
 // 这里调用pconline的接口
 string urlstr = "http://ip.taobao.com/service/getipinfo.php";
 // 从http://whois.pconline.com.cn取得ip所在的省市区信息
 string returnstr = this.getresult(urlstr, content, encodingstring);
 if (returnstr != null) {
 // 处理返回的省市区信息
 system.out.println(returnstr);
 string[] temp = returnstr.split(",");
 if(temp.length<3){
 return "0";//无效ip,局域网测试
 }
 string region = (temp[5].split(":"))[1].replaceall("\"", "");
 region = decodeunicode(region);// 省份

   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);// 国家
     break;
     case 3:
      area = (temp[i].split(":"))[1].replaceall("\"", "");
      area = decodeunicode(area);// 地区 
     break;
     case 5:
      region = (temp[i].split(":"))[1].replaceall("\"", "");
      region = decodeunicode(region);// 省份 
     break; 
     case 7:
      city = (temp[i].split(":"))[1].replaceall("\"", "");
      city = decodeunicode(city);// 市区
     break; 
     case 9:
       county = (temp[i].split(":"))[1].replaceall("\"", "");
       county = decodeunicode(county);// 地区 
     break;
     case 11:
      isp = (temp[i].split(":"))[1].replaceall("\"", "");
      isp = decodeunicode(isp); // isp公司
     break;
    }
   }

 system.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
 return region;
 }
 return null;
 }
 /**
 * @param urlstr
 *   请求的地址
 * @param content
 *   请求的参数 格式为:name=xxx&pwd=xxx
 * @param encoding
 *   服务器端请求编码。如gbk,utf-8等
 * @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();// 新建连接实例
 connection.setconnecttimeout(2000);// 设置连接超时时间,单位毫秒
 connection.setreadtimeout(2000);// 设置读取数据超时时间,单位毫秒
 connection.setdooutput(true);// 是否打开输出流 true|false
 connection.setdoinput(true);// 是否打开输入流true|false
 connection.setrequestmethod("post");// 提交方法post|get
 connection.setusecaches(false);// 是否缓存true|false
 connection.connect();// 打开连接端口
 dataoutputstream out = new dataoutputstream(connection
  .getoutputstream());// 打开输出流往对端服务器写数据
 out.writebytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
 out.flush();// 刷新
 out.close();// 关闭输出流
 bufferedreader reader = new bufferedreader(new inputstreamreader(
  connection.getinputstream(), encoding));// 往对端写完数据对端服务器返回数据
 // ,以bufferedreader流来读取
 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();// 关闭连接
 }
 }
 return null;
 }
 /**
 * unicode 转换成 中文
 *
 * @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();
 }
 // 测试
 public static void main(string[] args) {
 addressutils addressutils = new addressutils();
 // 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信
 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);
 // 输出结果为:广东省,广州市,越秀区
 }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网