淘宝(适用国内外城市)
一、请求接口(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<String,Object> requestMap = new HashMap<>();
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<String> list = JSON.parseArray(rspStr, String.class);
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": ""
}
三、频次限制
不详文章来源:https://www.toymoban.com/news/detail-770289.html
四、文档说明
http://whois.pconline.com.cn/#tabs-1文章来源地址https://www.toymoban.com/news/detail-770289.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 = "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<String,Object> requestMap = new HashMap<>();
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);
}
}
到了这里,关于分享几个IP获取地理位置的API的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!