ip地址归属地查询接口
一、百度
大公司,不必说,肯定好用,我就一直用的他作为主要查询接口。
url:http://opendata.baidu.com/api.php
params:query=ip,resource_id=6006,format=json
拼接之后url:
http://opendata.baidu.com/api.php?query=48.102.123.25&resource_id=6006&format=json
返回结果示例:
注意: format是返回结果,支持json,xml,json返回结果中,status=0表示请求成功,但是不表示有数据返回,data集合不为空才表示查询成功
二、太平洋
很好用,备用ip查询
http://whois.pconline.com.cn/ipJson.jsp?ip=xxx.xxx.xxx.xxx&json=true
返回结果示例:
如果请求的ip为不正确的ip,返回结果是本机ip的外网ip的归属地
更多详情请访问:http://whois.pconline.com.cn/
三、纯真
官网:http://www.cz88.net/
http://ipaddr.cz88.net/data.php?ip=47.102.123.89
返回结果示例:jsonp
注意: 偶尔会访问错误
非公网ip的ABC类地址
局域网地址范围分三类,以下IP段为内网IP段:
C类:192.168.0.0 - 192.168.255.255
B类:172.16.0.0 - 172.31.255.255
A类:10.0.0.0 - 10.255.255.255
环回地址:127.0.0.0-127.255.255.255
附上Java版本的非公网ip判断代码
public static boolean isLocalIp(String ip) {
String prefixA = "10.";
String prefixB = "172.";
String prefixC = "192.168.";
String prefixLH = "127.";
if (ip.startsWith(prefixC) || ip.startsWith(prefixA)) {
return true;
}
if (ip.startsWith(prefixB)) {
// 截取 第二个字节
String str = ip.substring(ip.indexOf(".") + 1);
int c = Integer.parseInt(str.substring(0, str.indexOf(".")));
return c > 15 && c < 32;
}
return ip.startsWith(prefixLH);
}
最后正则表达式版本:
public static boolean isLocalIp(String ip) {
String pattern = "^192.168.|^10.|^127.|^172.(1[6-9]|2[0-9]|3[0-1]).";
// 创建 Pattern 对象
Pattern p = Pattern.compile(pattern);
// 现在创建 matcher 对象
return p.matcher(ip).find();
}
注意:淘宝的api已经炸了