import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.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 = "http://poll.kuaidi100.com/poll/query.do";
/**
* 智能识别请求地址(固定值)
*/
private static final String DISCERN_URL = "http://www.kuaidi100.com/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 = MD5Util.encode(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 = (String)map.get("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("}");
return param.toString();
}
/**
* 向快递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 : params.entrySet()) {
if (builder.length() > 0) {
builder.append('&');
}
builder.append(URLEncoder.encode(param.getKey(), "UTF-8"));
builder.append('=');
builder.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] bytes = builder.toString().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);
conn.getOutputStream().write(bytes);
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response.toString();
}
//获取快递公司编码测试
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 = (String)map.get("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 = MessageDigest.getInstance("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;
}
}
}
快递100通过快递单号实时查询物流信息
内容版权声明:除非注明,否则皆为本站原创文章。