对接极兔快递订阅查询物流

1.申请注册极兔快递账号

极兔速递开放平台地址:极兔速递-开放平台

2.完善认证管理内容

3.获取apiAccount privateKey

 4.获取添加API接口  勾选需要的API

 5.订阅极兔快递

	//拼接订阅参数
	public JSONObject queryJTKD(Set<String> set) {
		Map<String, Object> bizContent = new HashMap<>();
		List<Map<String, String>> list = new ArrayList();
		set.stream().forEach(src -> {
			Map<String, String> params = new HashMap<String, String>();
			params.put("traceNode", "1&2&3&4&5&6&7&8&9&10&11");
			params.put("waybillCode", src); //"极兔运单编号"运单号个数超过1000
			list.add(params);
		});
		bizContent.put("Id", Constants.jtkdApiAccount);//"接入方在平台的api账户标识"
		bizContent.put("list", list);
		String json = JSONObject.toJSONString(bizContent);
		String res = HttpClientUtil.doPostJson(Constants.jtkdUrl+"/trace/subscribe",json , getHeadMap(json), null);
		JSONObject resObj = JSONObject.parseObject(res);
		return resObj;
	}

 6.查询极兔快递

	//拼接查询参数
	public JSONObject queryJtkdTrace(List<String> list) {//"极兔运单编号"运单号个数超过30
		StringBuilder sb = new StringBuilder();
		for (String expressNo :list) {
			sb.append(expressNo);
		}
		sb.deleteCharAt(sb.length() - 1);
		Map<String,String> stringMap = new HashMap<>();
		stringMap.put("billCodes",sb.toString());
		String json =JSONObject.toJSONString(stringMap);
		String res = HttpClientUtil.doPostJson(Constants.jtkdUrl+"/logistics/trace", json, getHeadMap(json), null);
		JSONObject resObj = JSONObject.parseObject(res);
		return resObj;
	}

  7.定义请求头及密钥

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

	//定义请求头
	public static Map<String, String> getHeadMap(String json) {
		Map<String, String> headMap = new HashMap<>();
		String digest = digestForHead(json, Constants.jtkdPrivateKey);
		headMap.put("digest", digest);//"签名字符串"
		headMap.put("apiAccount", Constants.jtkdApiAccount);//"接入方在平台的api账户标识"
		headMap.put("timestamp", System.currentTimeMillis() + "");
		return headMap;
	}
	//加密
	public static String digestForHead(String json, String privateKey) {
		String signStr = json + privateKey;
		byte[] encode = (new Base64()).encode(DigestUtils.md5(signStr));
		return new String(encode);
	}

 8.http请求方式

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


	private final static int DEFALUT_TIMEOUT = 5000;




	//http请求方式
	public static String doPostJson(String url, String json, Map<String, String> headers,Integer timeout) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			timeout = (timeout == null ? DEFALUT_TIMEOUT : timeout);
			RequestConfig requestConfig = RequestConfig.custom()
					.setConnectTimeout(timeout).setSocketTimeout(timeout).build();
			httpPost.setConfig(requestConfig);
			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
			// 创建请求内容
			if (null != headers && headers.size() > 0) {
				headers.keySet().forEach((key) -> {
					httpPost.addHeader(key, (String)headers.get(key));
				});
			}
			List<NameValuePair> nvp = new ArrayList();
			if (StringUtils.isNotEmpty(json)) {
				nvp.add(new BasicNameValuePair("bizContent", json));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nvp, "utf-8"));
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			log.error("HttpClient  doPostJson异常" + e.getMessage(), e);
		} finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

 9.设置配置项

    public static String jtkdUrl = "https://openapi.jtexpress.com.cn/webopenplatformapi/api";

    public static String jtkdApiAccount ="178337126125932605";

    public static String jtkdPrivateKey ="0258d71b55fc45e3ad7a7f38bf4b201a";

10.帮助中心

1.帮助中心有测试加密是否正确的工具,如果发现抓参后和工具的不一致,就要考虑是不是json转换异常等问题

2.如果项目的maven可以上传本地包,那么直接使用官方SDK就更方便了,里面已经封装好对应的接口

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值