这里我用的阿里淘宝的接口
先看看淘宝IP地址库
http://ip.taobao.com/instructions.html
直接在地址栏输入淘宝接口url请求一下看看效果:
http://ip.taobao.com/service/getIpInfo.php?ip=218.201.8.37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { code: 0, data: { ip: "218.201.8.37", country: "中国", area: "", region: "重庆", city: "重庆", county: "XX", isp: "移动", country_id: "CN", area_id: "", region_id: "500000", city_id: "500100", county_id: "xx", isp_id: "100025" } }
|
发现不管换啥ip,老是查询的area为空串,county显示xx,怀疑应该是地区area已经不支持查询,区县county可能是查询不到,或者为了保密被官方默认替换为xx?
所以以下测试代码就不获取上面两个参数了
如下测试基于SpringBoot,但是原理都一样
导入fastjson依赖处理json:
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency>
|
创建工具类AddressUtils :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package com.bhy702.website.common.util;
import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
public class AddressUtils { public static String getAddresses(String content, String encodingString){ String urlStr = "http://ip.taobao.com/service/getIpInfo.php"; String returnStr = getResult(urlStr, content,encodingString); if(returnStr != null){ return returnStr; } return null; }
private static String getResult(String urlStr, String content, String encodingString) { URL url = null; HttpURLConnection connection = null; try { url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(2000); connection.setReadTimeout(2000); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(content); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encodingString)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null){ buffer.append(line); } reader.close(); return buffer.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(connection != null){ connection.disconnect(); } } return null; } }
|
调用工具类AddressUtils查询ip地理位置信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.bhy702.website;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.bhy702.website.common.util.AddressUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @SpringBootTest public class WebsiteApplicationTests {
@Test public void getAddressByIp(){ String ip = "218.201.8.37"; String json_result = null; try { json_result = AddressUtils.getAddresses("ip=" + ip, "utf-8"); } catch (Exception e) { e.printStackTrace(); } JSONObject json = JSON.parseObject(json_result); if(json != null) { String country = json.getJSONObject("data").get("country").toString(); String province = json.getJSONObject("data").get("region").toString(); String city = json.getJSONObject("data").get("city").toString(); String isp = json.getJSONObject("data").get("isp").toString();
System.out.println("国家: " + country); System.out.println("省份: " + province); System.out.println("城市: " + city); System.out.println("互联网服务提供商: " + isp);
}else{ System.out.println("数据为null"); } } }
|
运行结果:
Author:
brbai
Permalink:
https://br-bai.github.io/2019/01/21/通过IP地址获取地理位置信息/
Slogan:
Make excellence a habit.