Express 100 real-time logistics information inquiry by express orders

 

Import com.alibaba.fastjson.JSON;
 Import java.io.BufferedReader;
 Import java.io.IOException;
 Import the java.io.InputStreamReader;
 Import java.net.HttpURLConnection The;
 Import the java.net.URL;
 Import java.net.URLEncoder ;
 Import java.security.MessageDigest;
 Import java.security.NoSuchAlgorithmException;
 Import classes in java.util *. ; 

/ ** 
 * express 100 real-time query logistics information tools
  * / 
public  class Express100Util { 

    / ** 
     * enterprise license key (in the courier 100 api get official website apply) 
     * / 
    Private  static Final String KEY = "*******" ;
     / ** 
     * enterprises number (100 in the courier's official website to obtain application api)
      * / 
    Private  static  Final String the CUSTOMER = "*********** " ; 

    / ** 
     * real address query request (fixed value) 
     * / 
    Private  static  Final String SYNQUERY_URL =" http://poll.kuaidi100.com/poll/query.do " ; 

    / ** 
     * smart identification request address (fixed value) 
     * / 
    Private  static  Final String DISCERN_URL = "http://www.kuaidi100.com/autonumber/auto" ; 

    / ** 
     * @Title: getExpressMessage 
     * @Description: Get Alert information 
     * @paramcom encoding Express 
     * @param NUM express a single 
     * @return String
      * / 
    public  static String getExpressMessage (com String, String NUM) { 

        // Get param 
        String param = getParam (com, NUM); 

        the Map <String, String> the params = new new the HashMap <String, String> (); 
        params.put ( "Customer" , the CUSTOMER);
         // for obtaining the MD5 signature sign, used to authenticate 
        String MD5Util.encode = sign (+ KEY + param the CUSTOMER); 
        the params .put ( "Sign" , Sign); 
        params.put ( "param", Param);
         // send a post request delivery information acquiring 
        String post = post (SYNQUERY_URL, the params);
         return post; 
    } 

    / ** 
     * Get Express coding 
   * @param NUM express orders
* / public static List <String> getCom (String NUM) { the Map <String, String> the params = new new the HashMap <String, String> (); params.put ( "NUM" , NUM); params.put ( "Key" , KEY); // Get the String express type set encoding: String = POST POST (DISCERN_URL, the 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:Open; 2: The opening of administrative resolution function and returns, the object and the current city information 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(); Private* / * 100 transmits a request to the post Alert/ ** } 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(! (= reader.readLine Line ()) = null ) { response.append (Line); } } the catch (Exception E) { e.printStackTrace (); } the finally { the try { IF ( null ! = Reader) { Reader .close (); } } the catch (IOException E) { e.printStackTrace (); } } return response.toString (); } // Get express encoding test 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 new the ArrayList <> (); System.out.println (MAPLIST); for (the Map <String, Object> Map: MAPLIST) { String comcode = (String) as map.get ( "comcode" ); CMS .add (comcode); } System.out.println (CMS); } // express check test / * public static void main (String [] args) { // express coding String = COM "Yunda"; // express single number String NUM = "3950055201640"; String expressMessage = getExpressMessage (COM, NUM); System.out.println(expressMessage); } * / } / ** * encrypted 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; } } }

Guess you like

Origin www.cnblogs.com/wanghj-15/p/12158004.html