Yuantong logistics track query (not a third party)

Preparation:

  1. Log in to the YTO Open Platform ( http://open.yto.net.cn/home ), apply for an account, and fill in the developer information
    insert image description here

  2. Add an api interface to obtain the account information of the production api interface

insert image description here

code show as below:

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 圆通物流查询类
 */
public class YTO {
    
    

	private static final Logger logger = LoggerFactory.getLogger(YTO.class);

	/** 签名格式 */
	public static final String SIGN_FOMART = "%sapp_key%sformat%smethod%stimestamp%suser_id%sv%s";

	/** 圆通物流URL */
	public static final String URL = "http://openapi.yto.net.cn/service/waybill_query/v1/rTVipn";

	/** 日期格式化 */
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) {
    
    
		YTO yto = new YTO();
		// 需替换为真实的生产参数
		String ret = yto.query("sF1Jzn", "JSON", "yto.Marketing.WaybillTrace", "YTOTEST", "1.01", "11QLlIZ",
				"YT88888888888");
		logger.info(ret);
	}

	/**
	 * 查询物流
	 * 
	 * @param appKey 分配给应用的客户编码(sF1Jzn)
	 * @param format param格式(JSON/XML)
	 * @param method 分配给用户的方法名(yto.Marketing.WaybillTrace)
	 * @param userId 用户在开放平台注册时填写的客户标识(YTOTEST)
	 * @param v API协议的版本号(1.01)
	 * @param scret 分配给用记的密钥(11QLlIZ)
	 * @param no
	 * @return
	 */
	public String query(String appKey, String format, String method, String userId, String v, String scret, String no) {
    
    
		String timestamp = dateFormat.format(new Date());
		// 组装
		String sign = String.format(SIGN_FOMART, scret, appKey, format, method, timestamp, userId, v);
		// MD5
		sign = encryption(sign);
		// 转大写
		if (StringUtils.isBlank(sign)) {
    
    
			return null;
		}
		sign = sign.toUpperCase();
		Map<String, String> params = new HashMap<>();
		params.put("sign", sign);
		params.put("app_key", appKey);
		params.put("format", format);
		params.put("method", method);
		params.put("timestamp", timestamp);
		params.put("user_id", userId);
		params.put("v", v);
		params.put("param", "[{\"Number\":\"" + no + "\"}]");
		try {
    
    
			return httpPost(URL, params);
		} catch (Exception e) {
    
    
			logger.error("查询圆通异常", e);
		}
		return null;

	}

	/***
	 * HttpPost请求
	 * 
	 * @param url
	 * @param requestParams
	 * @return
	 * @throws Exception
	 */
	public static String httpPost(String url, Map<String, String> requestParams) throws Exception {
    
    
		String result = null;

		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> params = new ArrayList<>();
		Iterator<Entry<String, String>> it = requestParams.entrySet().iterator();
		while (it.hasNext()) {
    
    
			Entry<String, String> en = it.next();
			String key = en.getKey();
			String value = en.getValue();
			if (value != null) {
    
    
				params.add(new BasicNameValuePair(key, value));
			}
		}
		httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		/** HttpResponse */
		try (CloseableHttpClient httpClient = HttpClients.createDefault();
				CloseableHttpResponse httpResponse = httpClient.execute(httpPost);) {
    
    
			HttpEntity httpEntity = httpResponse.getEntity();
			result = EntityUtils.toString(httpEntity, "UTF-8");
			EntityUtils.consume(httpEntity);
		} catch (Exception e) {
    
    
			logger.error("http请求异常", e);
		}
		return result;
	}

	/**
	 * 32位Md5加密
	 * 
	 * @param plainText
	 * @return
	 */
	public static String encryption(String plainText) {
    
    
		String reMd5 = null;
		try {
    
    
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuilder buf = new StringBuilder("");
			for (int offset = 0; offset < b.length; offset++) {
    
    
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			reMd5 = buf.toString();
		} catch (Exception e) {
    
    
			logger.error("md5加密异常", e);
		}
		return reMd5;
	}
}

Guess you like

Origin blog.csdn.net/wyanyi/article/details/114941905