根据IP获取地理位置
public static String getAddress(String ip) {
try {
URL realUrl = new URL("http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true");
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setReadTimeout(6000);
conn.setConnectTimeout(6000);
conn.setInstanceFollowRedirects(false);
int code = conn.getResponseCode();
StringBuilder sb = new StringBuilder();
String ipaddr = "";
if (code == 200) {
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));//指定编码格式
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println("sb:" + sb);
JSONObject jsonObject = JSON.parseObject(sb.toString());
System.out.println("json方式:" + jsonObject);
String pro = sb.substring((sb.indexOf("pro") + 6), sb.indexOf("proCode") - 3);
String city = sb.substring((sb.indexOf("city") + 7), sb.indexOf("cityCode") - 3);
String addr = (sb.substring(sb.indexOf("addr") + 7, sb.indexOf("regionNames") - 3)).trim();
String ipAddr = pro + city;
if (StringUtils.isBlank(ipAddr)) {
ipaddr = addr;
} else {
ipaddr = pro.equals(city) ? pro : pro + city;
}
}
return ipaddr;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String s = getAddress("30.233.96.241");
System.out.println(s);
}
输出
详解
http://whois.pconline.com.cn/ipJson.jsp?ip=xxx
这个接口是一个简单的IP查询接口,用于查询指定IP地址的地理位置信息。它可以通过GET请求,将需要查询的IP地址作为参数传递给接口,然后接口会返回包含该IP地址地理位置信息的JSON格式数据。
具体来说,该接口的URL是"http://whois.pconline.com.cn/ipJson.jsp?ip=",在URL的末尾需要拼接上要查询的IP地址。例如,如果要查询IP地址为"123.123.123.123"的地理位置信息,可以使用以下URL进行请求:
“http://whois.pconline.com.cn/ipJson.jsp?ip=123.123.123.123”
接口的返回结果中包含了IP地址的大概位置、所属地区、运营商等信息。请注意,这个接口可能是一个第三方服务,使用时需要遵守接口的使用规则和限制。
根据网址获取地理位置
代码实现和上述基本一致,不同的是根据网址获取IP,查询IP地理位置也更换为百度提供的接口
public static void main(String[] args) {
String hostname = "www.baidu.com"; // 替换为你要获取IP地址的网址
try {
InetAddress inetAddress = InetAddress.getByName(hostname);
String ipAddress = inetAddress.getHostAddress();
System.out.println("网址的IP地址为:" + ipAddress);
URL realUrl = new URL("https://qifu-api.baidubce.com/ip/geo/v1/district?ip=" + ipAddress);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setReadTimeout(6000);
conn.setConnectTimeout(6000);
conn.setInstanceFollowRedirects(false);
int code = conn.getResponseCode();
StringBuilder sb = new StringBuilder();
if (code == 200) {
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));//指定编码格式
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println("sb:" + sb);
JSONObject jsonObject = JSON.parseObject(sb.toString());
System.out.println(jsonObject);
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出
详解
https://qifu-api.baidubce.com/ip/geo/v1/district?ip=xxx
这个接口是百度云智能的IP地理位置查询接口。它可以通过GET请求,将需要查询的IP地址作为参数传递给接口,然后接口会返回包含该IP地址地理位置信息的JSON格式数据。
具体来说,该接口的URL是"https://qifu-api.baidubce.com/ip/geo/v1/district?ip=",在URL的末尾需要拼接上要查询的IP地址。例如,如果要查询IP地址为"123.123.123.123"的地理位置信息,可以使用以下URL进行请求:
“https://qifu-api.baidubce.com/ip/geo/v1/district?ip=123.123.123.123”
接口的返回结果中包含了IP地址所在的国家、省份、城市、区县等详细信息。这个接口是百度云提供的服务