import com.alibaba.fastjson.JSON; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURL ...
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; } } }