由于不知道什么时候接口会停止使用,还是推荐大家去购买快递接口。。。
快递110的开放接口 低调使用吧 免费的 亲测可用
代码:
/**
* 快递单号查询,使用快递110开放接口完成。
*
* @param expressNum 快递单号
* @return
* @throws IOException
*/
public static Map<String, Object> expressCheck(String expressNum) throws IOException {
// 快递110查询快递公司信息接口
String comCodeUrl = "http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=" + expressNum;
JSONObject jsonObject = Demo1.doGet(comCodeUrl);
if (jsonObject == null) {
System.out.println("查询失败!");
return new HashMap<>();
}
// 获取快递公司
String type = jsonObject.getJSONArray("auto").getJSONObject(0).getString("comCode");
// 快递110查询快递接口
String expressCheckUrl = "https://www.kuaidi100.com/query?type=" + type + "&postid=" + expressNum;
return Demo1.doGet(expressCheckUrl).getInnerMap();
}
主要是上面的两个地址
返回数据。
Demo1的doGet方法,就是最简单的一个httpclient get访问
// 方法参考http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html
public static JSONObject doGet(String uri) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
String s = EntityUtils.toString(entity1);
EntityUtils.consume(entity1);
response1.close();
return JSON.parseObject(s);
}
依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
还是那句话,自己测试使用吧,如果公司的生产环境不推荐使用这种,不稳定因素太大。。而且一分钱查询一次的接口费用,对公司来说,负担应该不算大。。。