Java ip地址查询,根据ip接口获得ip所在省市区,邮编,运营商等
早上一来,项目经理就说需要添加一个用户ip归属地查询功能,然后在网上搜罗半天,研究出一个比较简单的方法,通过接口返回地址json数据
有百度接口,新浪接口,这里用的是淘宝ip接口
通过淘宝IP地址库获取IP位置
请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
响应信息:(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商等
2018.2.26运行项目发现淘宝接口报错502,于是找到一个替代的接口,新浪接口
String sip = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";
直接将淘宝接口 换成这个就可以运行,过程一样
2018/6/19 还是用淘宝ip接口
public static void main(String[] args) {
// 测试ip 221.232.245.73 湖北武汉
String ip = "221.232.245.73";
String address = "";
try {
address = addressUtils.getAddresses("ip="+ip, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(address);
// 输出结果为:中国 湖北省 武汉市
}
/** 3 * @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("IP====="+returnStr);
String[] temp = returnStr.split(",");
if(temp.length<3){
return "0"; //无效IP,局域网测试
}
String region = (temp[5].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region); // 省
System.out.println("region = "+region);
String country = "";
String area = "";
// String region = "";
String city = "";
String county = "";
String isp = "";
System.out.println("temp的长度="+temp.length);
for (int i = 0; i < temp.length; i++) {
switch (i) {
如果使用的是新浪的接口,那这里的需要修改,case:3 4 5分别对应国家,省,市区
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();
}
输出结果为
IP====={"code":0,
"data":{
"ip":"221.232.245.73", //ip
"country":"中国", //国家
"area":"", //区
"region":"湖北", //省
"city":"武汉", //市
"county":"XX",
"isp":"电信", //宽带运营
"country_id":"CN", //国家id
"area_id":"", //区编码
"region_id":"420000", //省编码
"city_id":"420100", //市编码
"county_id":"xx",
"isp_id":"100017"}}
region = 武汉
temp的长度=14
中国==武汉=电信==420100
Java ip地址查询,根据ip接口获得ip所在省市区,邮编,运营商等的更多相关文章
- 通过Web Service实现IP地址查询功能
实例01 实现一个简单的Web服务访问 本实例将实现IP地址查询接口服务,根据用户传入的IP地址返回IP所在的省.市.地区,实例中将会用到IP地址库用于查询信息,由于数据较多,所以读者可在光盘资源文件 ...
- IP地址查询接口及调用方法
1.查询地址 搜狐IP地址查询接口(IP):http://pv.sohu.com/cityjson 1616 IP地址查询接口(IP+地址):http://w.1616.net/chaxun/ipto ...
- IP地址查询接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- 齐全的IP地址查询接口及调用方法(转)
设计蜂巢IP地址查询接口:http://www.hujuntao.com/api/ip/ip.php 腾讯IP地址查询接口:http://fw.qq.com/ipaddress 新浪IP地址查询接口: ...
- API之IP地址查询---权威的IP地址查询接口集合
原文地址:http://yushine.iteye.com/blog/1717586 推荐实用IP138 http://www.baidu.com/s?wd=IP&rsv_spt=1& ...
- 分享几个免费IP地址查询API接口
几个免费IP地址查询API接口 1.IP地址查询接口:http://apis.juhe.cn/ip/ip2addr要先去https://www.juhe.cn/docs/api/...申请APPKEY ...
- 几个免费IP地址查询API接口
转:http://blog.csdn.net/ishxiao/article/details/52670242 -------------------------------------------- ...
- [1]IP地址查询
今天起开始玩百度APIStore里面的免费API.以前用过的有12306的:数据.接口,有时间整理出来,12306的有点乱就是了.还有扇贝以及有道的API,之前用在留言板里自动翻译,公司用过百度地图以 ...
- Android中由IP地址查询经纬度坐标的实例
大家都知道,根据IP地址就可以知道它所在的具体位置,在Android中同样可以由IP地址得到它的位置,即具体的地理经纬度坐标. 本文就直接以代码的方式演示如何根据IP地址查询地理经纬度坐标位置,下面的 ...
随机推荐
- go 内嵌对象类型
demo1 // Sample program to show how to embed a type into another type and // the relationship betwee ...
- .net core 基础知识
1.IOC(转:https://www.cnblogs.com/artech/p/inside-asp-net-core.html) IoC的全名Inverse of Control,翻译成中文就是“ ...
- kbengine学习1 安装
KBengine一年前就知道了,但是没来得及学(只记得是C++ + python脚本),前一个项目unity3d+fkask+socketio+sqlite硬怼出来的.这半年也没来得及管.(好像当时看 ...
- win10如何一键开启关闭windows Defender(亲测有效)
win10如何一键开启关闭windows Defender(亲测有效) 一.总结 一句话总结:各种找资料如何开启关闭都没用,直接下载软件简单方便 软件 因为我关windows defender是用的一 ...
- 接口测试——带token请求post接口(postman学习)
今天遇到一个接口,是添加备注的,post类型,访问参数中需要带上token才行,我在header 中直接加token参数,接口总返回 403,请登陆 1.考虑yapi接口平台集成的是postman的接 ...
- 自己动手编写IPv4地址包含关系测试的小脚本
工作中需要对地址包含关系进行测试,现有ipaddress标准库和IPy无法满足,于是自己动手编写小脚本,主要实现== , in, <等专用功能,其他功能可以后续用到再补充,例如迭代打印网段内所有 ...
- Django ModelForm 组件的应用
ModelForm组件的应用 ModelForm 组件的创建: 1.创建一个类,该类继承 forms.ModelForm 2.大致组成部分 class ModelNameModelForm(form ...
- vux 给元素动态添加css
<template> <div class="jdtI" :style="{styleObj}"></div> </t ...
- 微信小程序地图demo完整
<block wx:for="{{data_2}}" wx:key='index' wx:if="{{data_2.length}}"> <v ...
- CentOS6.8环境下搭建yum网络仓库
CentOS6.8环境下搭建yum网络仓库 本文利用ftp服务,在CentOS6.8系统下搭建一个yum仓库,然后用另一台虚拟机访问该仓库.并安装程序包 安装ftp服务 查询ftp服务是否安装 [ro ...