快递100通过快递单号实时查询物流信息import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.HttpURLConnection;
import java.URL;
import java.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
* 快递100实时查询物流信息⼯具类
*/
public class Express100Util {
/**
* 企业授权key(在快递100官⽹申请api获取
*/
private static final String KEY="*******";
/**
* 企业公司编号(在快递100官⽹申请api获取)
*/
private static final String CUSTOMER="***********";
/
**
* 实时查询请求地址(固定值)
*/
查快递物流到哪了private static final String SYNQUERY_URL = "poll.kuaidi100/poll/query.do";
/**
* 智能识别请求地址(固定值)
*/
private static final String DISCERN_URL = "www.kuaidi100/autonumber/auto";
/**
* @Title: getExpressMessage
* @Description: 获取快递信息
* @param com 快递公司编码
* @param num 快递单号
* @return String
*/
public static String getExpressMessage(String com,String num){
//获取param
String param = getParam(com, num);
Map<String, String> params = new HashMap<String, String>();
params.put("customer",CUSTOMER);
//进⾏MD5加密获取sign签名,⽤于验证⾝份
String sign = de(param + KEY + CUSTOMER);
params.put("sign", sign);
params.put("param", param);
//发送post请求获取快递信息
String post = post(SYNQUERY_URL,params);
return post;
}
/**
* 获取快递公司编码
   * @param num 快递单号
*/
public static List<String> getCom(String num){
Map<String, String> params = new HashMap<String, String>();
params.put("num",num);
params.put("key",KEY);
//获取到String类型的快递公司编码集合:
String post = post(DISCERN_URL, params);
//
List<Map<String, Object>> mapList = (List<Map<String, Object>>) JSON.parse(post);
ArrayList<String> coms = new ArrayList<>();
System.out.println(mapList);
for (Map<String, Object> map : mapList) {
String comCode = (("comCode");
coms.add(comCode);
}
return coms;
}
/**
* 获取param
*/
private static String getParam(String com,String num){
int resultv2 = 0;    //⾏政区域解析 0:关闭(默认); 1:开通; 2:开通⾏政解析功能并且返回出发、⽬的及当前城市信息
StringBuilder param = new StringBuilder("{");
param.append("\"com\":\"").append(com).append("\"");
param.append(",\"num\":\"").append(num).append("\"");
if(1 == resultv2) {
param.append(",\"resultv2\":1");
} else {
param.append(",\"resultv2\":0");
}
param.append("}");
String();
}
/**
* 向快递100发送post请求
*/
private static String post(String reqUrl,Map<String, String> params) {
StringBuffer response = new StringBuffer("");
BufferedReader reader = null;
try {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> param : Set()) {
if (builder.length() > 0) {
builder.append('&');
}
builder.Key(), "UTF-8"));
builder.append('=');
builder.de(String.Value()), "UTF-8"));
}
byte[] bytes = String().getBytes("UTF-8");
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
conn.setDoOutput(true);
reader = new BufferedReader(new InputStream(), "UTF-8"));
String line = "";
while ((line = adLine()) != null) {
response.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
String();
}
/
/获取快递公司编码测试
public static void main(String[] args) {
Map<String, String> params = new HashMap<String, String>();
params.put("num","3950055201640");
params.put("key",KEY);
//        String post = post(DISCERN_URL, params);
String post = "[{\"lengthPre\":13,\"comCode\":\"yunda\",\"noPre\":\"395\",\"noCount\":36463},{\"lengthPre\":13,\"comCode\":\"shunfen\",\"noPre\":\"395\",\"noCount\":36463}]";        System.out.println(post);
List<Map<String, Object>> mapList = (List<Map<String, Object>>) JSON.parse(post);
ArrayList<String> cms = new ArrayList<>();
System.out.println(mapList);
for (Map<String, Object> map : mapList) {
String comCode = (("comCode");
cms.add(comCode);
}
System.out.println(cms);
}
//快递查询测试
/*public static void main(String[] args) {
//快递公司编码
String com = "yunda";
//快递单号
String num = "3950055201640";
String expressMessage = getExpressMessage(com, num);
System.out.println(expressMessage);
}*/
}
/**
* md5加密
*/
class MD5Util {
private static MessageDigest mdigest = null;
private static char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static MessageDigest getMdInst() {
if (null == mdigest) {
try {
mdigest = Instance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return mdigest;
}
public static String encode(String s) {
if(null == s) {
return "";
}
try {
byte[] bytes = s.getBytes();
getMdInst().update(bytes);
byte[] md = getMdInst().digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = digits[byte0 >>> 4 & 0xf];
str[k++] = digits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}