淘宝(适用国内外城市)
一、请求接口(GET):
https://ip.taobao.com/outGetIpInfo?ip=IP地址&accessKey=alibaba-inc
二、返回数据格式:
{
"data": {
"area": "",
"country": "中国",
"isp_id": "100017",
"queryIp": "175.9.142.21",
"city": "长沙",
"ip": "175.9.142.21",
"isp": "电信",
"county": "",
"region_id": "430000",
"area_id": "",
"county_id": null,
"region": "湖南",
"country_id": "CN",
"city_id": "430100"
},
"msg": "query success",
"code": 0
}
三、频次限制:
每个用户的访问频率需小于1qps
四、文档说明:
http://ip.taobao.com/instructions.html
五、代码片段:
package com.example.animalhome.utils;
import com.alibaba.fastjson.JSONObject;
import com.example.animalhome.utils.HttpClient.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* 获取地址类
*/
public class AddressUtils {
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
// IP地址查询
public static final String IP_URL = "https://ip.taobao.com/outGetIpInfo";
// 未知地址
public static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip) {
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip)) {
return "内网IP";
}
try {
Map
requestMap.put("ip",ip);
requestMap.put("accessKey","alibaba-inc");
String rspStr = HttpClientUtil.httpClienOfGet(IP_URL, requestMap);
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常:{}", ip);
return address;
}
JSONObject obj = JSONObject.parseObject(rspStr);
String code = obj.getString("code");
if (StringUtils.equals(code,"0")){
JSONObject data = obj.getJSONObject("data");
String region = data.getString("region");
String city = data.getString("city");
String country = data.getString("country");
address = StringUtils.equalsAny("XX",region,city) ? country : String.format("%s %s", region, city);
}
} catch (Exception e) {
log.error("获取地理位置异常", e);
}
return address;
}
public static void main(String[] args) {
String realAddressByIP = getRealAddressByIP("175.9.142.21");
System.out.println("归属地:" + realAddressByIP);
}
}
IPIP(适用国内外城市)
一 、请求接口(GET):
http://freeapi.ipip.net/ip地址字串
二、返回数据格式:
["中国","广东","广州","","珠江宽频/联通"]
三、频次限制:
每天1000次
四、文档说明:
https://www.ipip.net/support/api.html
五、代码片段
package com.example.animalhome.utils;
import com.alibaba.fastjson.JSON;
import com.example.animalhome.utils.HttpClient.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* 获取地址类
*/
public class AddressUtils {
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
// IP地址查询
public static final String IP_URL = "http://freeapi.ipip.net/";
// 未知地址
public static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip) {
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip)) {
return "内网IP";
}
try {
String rspStr = HttpClientUtil.httpClienOfGet(IP_URL + ip, null);
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常:{}", ip);
return address;
}
List
address = StringUtils.isEmpty(list.get(2)) ? list.get(0) : String.format("%s %s", list.get(1), list.get(2));
} catch (Exception e) {
log.error("获取地理位置异常", e);
}
return address;
}
public static void main(String[] args) {
String realAddressByIP = getRealAddressByIP("175.9.142.21");
System.out.println("归属地:" + realAddressByIP);
}
}
太平洋网络IP地址查询Web接口(适用国内外城市)
一、请求接口(GET):
http://whois.pconline.com.cn/ipJson.jsp?ip=IP地址&json=true
二、返回数据格式
{
"ip": "175.9.142.21",
"pro": "湖南省",
"proCode": "430000",
"city": "长沙市",
"cityCode": "430100",
"region": "",
"regionCode": "0",
"addr": "湖南省长沙市 电信",
"regionNames": "",
"err": ""
}
三、频次限制
不详
四、文档说明
http://whois.pconline.com.cn/#tabs-1
五、代码片段
package com.example.animalhome.utils;
import com.alibaba.fastjson.JSONObject;
import com.example.animalhome.utils.HttpClient.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* 获取地址类
*/
public class AddressUtils {
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
// IP地址查询
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
// 未知地址
public static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip) {
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip)) {
return "内网IP";
}
try {
Map
requestMap.put("ip",ip);
requestMap.put("json",true);
String rspStr = HttpClientUtil.httpClienOfGet(IP_URL,requestMap );
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常:{}", ip);
return address;
}
JSONObject obj = JSONObject.parseObject(rspStr);
String region = obj.getString("pro");
String city = obj.getString("city");
String addr = obj.getString("addr");
address = StringUtils.isEmpty(region) && StringUtils.isEmpty(city) ? addr.replaceAll(" ","") : String.format("%s %s", region, city);
} catch (Exception e) {
log.error("获取地理位置异常", e);
}
return address;
}
public static void main(String[] args) {
String realAddressByIP = getRealAddressByIP("175.9.142.21");
System.out.println(realAddressByIP);
}
————————————————
版权声明:本文为CSDN博主「超级无敌小神仙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44825912/article/details/129715526