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已經炸了