Java对接快递100

快递物流接口文档_电子面单接口文档_快递100api接口文档

API调试工具-快递100API开放平台

企业管理后台


#快递100配置
express:
  url: https://poll.kuaidi100.com/poll/query.do
  mapUrl: https://poll.kuaidi100.com/poll/maptrack.do
  key: xxxxxxxx
  customer: xxxxxxxxxxxxxxxxxxxxxxxx



/**
 * 快递100查询
 *
 * @author: cyl
 * @date: 2022-07-03 09:59
 */
@Slf4j
public class ExpressUtil {


    @Value("${express.url}")
    private static String URL;

    @Value("${express.mapUrl}")
    private static String MAP_URL;

    @Value("${express.key}")
    private static String KEY;

    @Value("${express.customer}")
    private static String CUSTOMER;

    private static final String SUCCESS_STATUS = "200";


    /**
     * 查询实时快递
     *
     * @param expressNo        快递单号
     * @param expressCompanyNo 快递公司编码
     * @param mobile           收、寄件人的电话号码
     * @return
     */
    public static JSONArray queryExpress(String expressNo, String expressCompanyNo, String mobile) {
        JSONObject param = new JSONObject();
        param.put("com", expressCompanyNo);
        param.put("num", expressNo);
        param.put("phone", mobile);
        String sign = SecureUtil.md5(param.toJSONString() + KEY + CUSTOMER).toUpperCase();

        Map<String, Object> map = Maps.newHashMap();
        map.put("customer", CUSTOMER);
        map.put("sign", sign);
        map.put("param", param.toJSONString());

        String result = HttpRequest.post(URL)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .timeout(20000)
                .form(map)
                .execute()
                .body();

        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.getString("status");
        if (StrUtil.isBlank(status) || !SUCCESS_STATUS.equals(status)) {
            log.error("快递100查询异常:{}", result);
            return new JSONArray();
        }

        return jsonObject.getJSONArray("data");
    }

    /**
     * 查询地图轨迹
     *
     * @param expressNo        快递单号
     * @param expressCompanyNo 快递公司编码
     * @param from              出发地信息,最小颗粒到市级,例如:广东省深圳市
     * @param to                目的地信息,最小颗粒到市级,例如:广东省深圳市
     * @return
     */
    public static JSONArray queryExpressMap(String expressNo, String expressCompanyNo, String from, String to) {
        JSONObject param = new JSONObject();
        param.put("com", expressCompanyNo);
        param.put("num", expressNo);
        param.put("from", from);
        param.put("to", to);
        String sign = SecureUtil.md5(param.toJSONString() + KEY + CUSTOMER).toUpperCase();

        Map<String, Object> map = Maps.newHashMap();
        map.put("customer", CUSTOMER);
        map.put("sign", sign);
        map.put("param", param.toJSONString());

        String result = HttpRequest.post(MAP_URL)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .timeout(20000)
                .form(map)
                .execute()
                .body();

        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.getString("status");
        if (StrUtil.isBlank(status) || !SUCCESS_STATUS.equals(status)) {
            log.error("快递100查询异常:{}", result);
            return new JSONArray();
        }

        return jsonObject.getJSONArray("data");
    }

}


版权声明:本文为weixin_44765388原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。