调用第三方接口-中通-圆通-申通

一、中通快递。对接 获取快件轨迹信息 接口

中通开放平台地址:https://zop.zto.com/apiDoc/

主动查询 对接文档:

image

要注意几个点:

1、Content-Type的类型 本接口为:application/x-www-form-urlencoded

2、消息内容格式:按要求将 参数信息 依次填充好。

    根据 “返回参数描述”,可将参数放置在 Map中进行流程传递。

    即

Map parameters =new HashMap<>();

parameters.put("data", orderNo.toString());

parameters.put("company_id", CompanyId);

parameters.put("msg_type", msg_type_search);

3、注意加密方式。

image

将参数转成 key=value&key=value....+“签名key” 形式的字符串。 MD5 -> Base64


//将Map转为 key=value&key=value

public static StringparamsToQueryString(Map params) {

return params.entrySet().stream().map(e -> e.getKey() +"=" + e.getValue()).collect(Collectors.joining("&"));

}

4、最后 post。将数据传输出去。


完整代码:


public class ZhongTongAPI {

//测试环境

    private static final StringCompanyId ="kfpttestCode";

    private static final StringKey ="kfpttestkey==";

    private static final StringURL ="http://58.40.16.122:8080/traceInterfaceNewTraces"; // 查询

    private static final Stringmsg_type_search ="NEW_TRACES";

    public static void main(String[] args)throws Exception {

            ZhongTongAPI z =new ZhongTongAPI();

            List no =new ArrayList<>();

            no.add("680000000001");

            no.add("680000000000");

            no.add("600000000");

            Map zt = z.searchByBillCode(no);

        System.out.println("查询结果:" + zt);

}

/**

* 查询订单物流轨迹接口 通过订单号 集合,List<订单号>

*

* @param orderNo

* @return

* @throws Exception

*/

public Map searchByBillCode(List orderNo)throws Exception {

if (orderNo ==null) {

throw new Exception("请求内容不能为空!");

    }

Map parameters =new HashMap<>();

    parameters.put("data", orderNo.toString());

    parameters.put("company_id", CompanyId);

    parameters.put("msg_type", msg_type_search);

    String content =paramsToQueryString(parameters);

    String strToDigest = encrypt(content, Key, "utf-8");

    String ret =post(URL,parameters,strToDigest);

    System.out.println("查询字符串:"+ret);

    return ObjectTransfer.mapValue(ret);

}

private static String post(String url, Map parameters, String dataDigest)throws Exception {

if (parameters ==null) {

parameters =new HashMap<>();

    }

HttpURLConnection connection =null;

    InputStream is =null;

    OutputStream os =null;

    BufferedReader br =null;

    String result =null;

    try {

URL realUrl =new URL(url);

        connection = (HttpURLConnection) realUrl.openConnection();

        connection.setRequestMethod("POST");

        connection.setConnectTimeout(15000);

        connection.setReadTimeout(30000);

        connection.setDoOutput(true);

        connection.setDoInput(true);

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.setRequestProperty("x-datadigest", dataDigest);

        connection.setRequestProperty("x-companyid", CompanyId);

        os = connection.getOutputStream();

        System.out.println("传输数据:"+paramsToQueryStringUrlencoded(parameters));

        os.write(paramsToQueryStringUrlencoded(parameters).getBytes(Charset.forName("UTF-8")));

        if (connection.getResponseCode() ==200) {

is = connection.getInputStream();

            br =new BufferedReader(new InputStreamReader(is, "UTF-8"));

            StringBuffer sbf =new StringBuffer("");

            String temp =null;

            while ((temp = br.readLine()) !=null) {

sbf.append(temp);

            }

result = sbf.toString();

        }

}catch (Exception e) {

throw e;

    }finally {

// 关闭资源

        if (null != br) {

try {

br.close();

            }catch (IOException e) {

e.printStackTrace();

            }

}

if (null != os) {

try {

os.close();

            }catch (IOException e) {

e.printStackTrace();

            }

}

if (null != is) {

try {

is.close();

            }catch (IOException e) {

e.printStackTrace();

            }

}

if (null != is) {

connection.disconnect();

        }

}

return result;

}

//将map转化为key=value&的形式

public static String paramsToQueryString(Map params) {

return params.entrySet().stream().map(e -> e.getKey() +"=" + e.getValue()).collect(Collectors.joining("&"));

}

/**

* 签名生成 MD5+Baes64

* @param content  内容

* @param keyValue key

* @param charset  编码方式

*/

private String encrypt(String content, String keyValue, String charset)throws Exception {

String strToDigest = content + keyValue;

    MessageDigest md = MessageDigest.getInstance("MD5");

    md.update(strToDigest.getBytes(Charset.forName(charset)));

    return Base64.getEncoder().encodeToString(md.digest());

}

}

你可能感兴趣的:(调用第三方接口-中通-圆通-申通)