一 点睛
1 通过调用快递API接口,查询快递信息。
2 官网
3 API接口文档
https://api.kuaidi100.com/help/doc/?code=5f0ffa8f2977d50a94e1023c&openKey=快递信息推送服务
二 代码
package com.imooc.demo.common.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import java.util.HashMap;
import java.util.Map;
/**
* @className: Kuaidi100Utils
* @description: 快递查询工具类
* 官网:https://www.kuaidi100.com/
* API接口说明:https://api.kuaidi100.com/help/doc/?code=5f0ffa8f2977d50a94e1023c&openKey=快递信息推送服务
* @date: 2020/8/20
* @author: cakin
*/
public class KuaidiUtils {
/**
* 账号信息需要去官网申请
*/
private static String key = "XXXXXXXXXXXXXXX";
/**
* 账号信息需要去官网申请
*/
private static String customer ="YYYYYYYYYYYYYYYYY";
/**
* 免费物流接口
* @param comCode 快递公司编码
* @param postId 快递单号
* @return JSONObject JSON对象
*/
public static JSONObject freeLogistics(String comCode,String postId){
String url = "https://www.kuaidi100.com/query?type="+comCode+"&postid="+postId;
String result = HttpClientUtils.sendGetRequest(url, null);
System.out.println(url);
System.out.println("====================================================");
System.out.println(result);
return JSON.parseObject(result);
}
/**
* 物流接口
* @param comCode 快递公司编码
* @param postId 快递单号
* @return JSONObject JSON对象
*/
public static JSONObject logistics(String comCode,String postId){
String param ="{\"com\":\""+comCode+"\",\"num\":\""+postId+"\",\"from\":\"\",\"to\":\"\",\"resultv2\":0}";
String sign = DigestUtils.md5Hex(param+key+customer);
Map<String,String> params = new HashMap<>();
params.put("param",param);
params.put("sign",sign.toUpperCase());
params.put("customer",customer);
String url = "http://poll.kuaidi100.com/poll/query.do";
String result = "";
try {
result = HttpClientUtils.sendPostRequest(url, params, null, null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(url);
System.out.println("====================================================");
System.out.println(result);
return JSON.parseObject(result);
}
public static void main(String[] agrs){
KuaidiUtils.freeLogistics("yunda","3556090145441");
System.out.println("--------------------------------------------------------------------------------------");
KuaidiUtils.logistics("yunda", "3556090145441");
}
}
三 测试
https://www.kuaidi100.com/query?type=yunda&postid=3556090145441
====================================================
null
--------------------------------------------------------------------------------------
http://poll.kuaidi100.com/poll/query.do
====================================================
{"message":"ok","nu":"3556090145441","ischeck":"1","condition":"D01","com":"yunda","status":"200","state":"3","data":[...............]}
Process finished with exit code 0