Express recognition api single query interface calls docking demo

Not long ago, calling himself docking realized through courier api function, if it was found to express other re-docking, is it will waste too much time, this interface docking logistics is a problem, either individually need to connect a number of courier companies each ship carried a docking courier company will develop more than a dozen interfaces, cumbersome and complex development effort.

So choose a third-party courier API is the most reasonable, given below express the birds api design implementation of the interface.

Source Address:

https://github.com/lzy2626/LogisticsInfoTest

"Instant Search" interface:

RequestData and DataSign are data signature. The other three are fixed wording.

 Express birds

    Express Logistics birds api to achieve real-time query

    1. First of all birds have a courier account, in accordance with the other requirements, improve the user application.

    2. according to their needs, service provisioning

  3. Go to "My API interface," according to "Interface" in the development documentation for design requirements, download the official demo, edit their own code.

demo request data:

com.zs.app Package; 

Import java.io.BufferedReader; 
Import java.io.IOException; 
Import the java.io.InputStreamReader; 
Import java.io.OutputStreamWriter; 
Import java.io.UnsupportedEncodingException; 
Import java.net.HttpURLConnection The; 
Import java.net.URL; 
Import java.net.URLEncoder; 
Import java.security.MessageDigest; 
Import java.util.HashMap; 
Import java.util.Map; 

/ ** 
 * 
 * express logistics birds trajectory instant query interface 
 * 
 * @ technology QQ group: 456 320 272 
 * @see: http://www.kdniao.com/YundanChaxunAPI.aspx 
 * @copyright: Shenzhen City Express gold data services Limited 
 * 
 * DEMO private electricity provider ID and the test use only, Please separate account registered a formal environment 
 * a single day more than 500 single query volume, we recommend that the logistics track access subscription push Interface
 * 
    // url request
 * ID and Key to go to the official website application: HTTP: //www.kdniao.com/ServiceApply.aspx 
 * / 

public class KdniaoTrackQueryAPI { 
    
    // the DEMO 
    public static void main (String [] args) { 
        KdniaoTrackQueryAPI KdniaoTrackQueryAPI new new API = (); 
        {the try 
            String api.getOrderTracesByJson Result = ( "the ANE", "210 001 633 605"); 
            of System.out.print (Result); 
            
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
    } 
    
    // electricity supplier ID 
    Private String EBusinessID = "1330422"; 
    // private key encryption electricity supplier, providing courier birds, take good care not to leak 
    private String AppKey = "7611818b-6279-4398-8747 -df2ca39e86b4";
    private String ReqURL="http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";   
 
    /**
     * Json方式 查询订单物流轨迹
     * @throws Exception 
     */
    public String getOrderTracesByJson(String expCode, String expNo) throws Exception{
        String requestData= "{'OrderCode':'','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
        
        Map<String, String> params = new HashMap<String, String>();
        params.put("RequestData", urlEncoder(requestData, "UTF-8"));
        params.put("EBusinessID", EBusinessID);
        params.put("RequestType", "1002");
        String dataSign=encrypt(requestData, AppKey, "UTF-8");
        params.put ( "DataSign", URLEncoder (dataSign, "UTF-. 8")); 
        params.put("DataType", "2");
        
        String Result = sendPost (reqURL, the params); 
        
        // return information in accordance with the business process ...... 
        
        return Result; 
    } 
 
    / ** 
     * the MD5 encrypted 
     * @param str content        
     * @param charset coding 
     * @throws Exception 
     * / 
    @SuppressWarnings ( "unused") 
    Private String the MD5 (STR String, String charset) throws Exception { 
        the MessageDigest MessageDigest.getInstance MD = ( " the MD5 "); 
        md.update (str.getBytes (charset)); 
        byte [] Result = md.digest (); 
        the StringBuffer the StringBuffer new new SB = (32); 
        for (int I = 0; I <result.length;i++) { 
            int Val = Result [I] & 0xFF; 
            IF (Val <= 0xF) { 
                sb.append ( "0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }
    
    /**
     * base64编码
     * @param str 内容       
     * @param charset 编码方式
     * @throws UnsupportedEncodingException 
     */
    private String base64(String str, String charset) throws UnsupportedEncodingException{
        String encoded = base64Encode(str.getBytes(charset));
        return encoded;    
    }   
    
    @SuppressWarnings("unused")
    private String urlEncoder(String str, String charset) throws UnsupportedEncodingException{
        String result = URLEncoder.encode(str, charset);
        return result;
    }
    
    /**
     * Sign signature generation electricity supplier 
     * @param content content    
     * @param keyValue AppKey   
     * @param charset encoding 
     * @throws UnsupportedEncodingException, Exception 
     * @return DataSign signature 
     * / 
    @SuppressWarnings ( "unused") 
    Private String the encrypt (String Content, String the keyValue, String charset) throws UnsupportedEncodingException, Exception 
    { 
        IF (the keyValue = null)! 
        { 
            return Base64 (the MD5 (the keyValue + Content, charset), charset); 
        }
        return Base64 (the MD5 (Content, charset), charset); 
    } 
    
     / ** 
     * POST method request sent to the specified the URL      
     the URL transmission request URL * @param     
     * @param request parameter set the params      
     * @return remote resources in response to the results 
     * / 
    @SuppressWarnings ( "unused") 
    Private sendPost String (String URL, the Map <String, String> the params) { 
        the OutputStreamWriter OUT = null; 
        the BufferedReader in = null;         
        the StringBuilder the StringBuilder new new Result = (); 
        the try { 
            the URL = new new realUrl the URL (URL); 
            the HttpURLConnection Conn = (the HttpURLConnection) realUrl.openConnection (); 
            / / POST request must send the following two lines disposed 
            conn.setDoOutput (to true); 
            conn.setDoInput (to true); 
            / / POST method 
            conn.setRequestMethod ( "the POST"); 
            // common setting request attribute
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("MessContent-Type", "application/x-www-form-urlencoded");
            conn.connect();
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // 发送请求参数            
            if (params != null) {
                  StringBuilder param = new StringBuilder(); 
                  for (Map.Entry<String, String> entry : params.entrySet()) {
                      if(param.length()>0){
                          param.append("&");
                      }               
                      param.append(entry.getKey());
                      param.append("=");
                      param.append(entry.getValue());                     
                      //System.out.println(entry.getKey()+":"+entry.getValue());
                  }
                  //System.out.println("param:"+param.toString());
                  out.write(param.toString());
            }
            // flush输出流的缓冲
            out.flush();
            // definition of the input stream BufferedReader read the URL of the response
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append (Line); 
            } 
        } the catch (Exception E) {             
            e.printStackTrace (); 
        } 
        // finally block used to close the output stream, the input stream 
        finally { 
            the try { 
                IF (OUT = null!) { 
                    the out.close ( ); 
                } 
                IF (in = null) {! 
                    in.close (); 
                } 
            } 
            the catch (IOException EX) { 
                ex.printStackTrace (); 
            }
        } 
        return result.toString (); 
    } 
    
    
    Private static char [] = new new base64EncodeChars char [ ] {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
        'w', 'x', 'y', 'z', '0', '1', '2', '3', 
        '4', '5', '6', '7', '8', '9', '+', '/' }; 
    
    public static String base64Encode(byte[] data) { 
        StringBuffer sb = new StringBuffer(); 
        int len = data.length; 
        int i = 0; 
        int b1, b2, b3; 
        while (i < len) { 
            b1 = data[i++] & 0xff; 
            if (i == len) 
            { 
                sb.append(base64EncodeChars[b1 >>> 2]); 
                sb.append(base64EncodeChars[(b1 & 0x3) << 4]); 
                sb.append("=="); 
                break; 
            } 
            b2 = data[i++] & 0xff; 
            if (i == len) 
            { 
                sb.append(base64EncodeChars[b1 >>> 2]); 
                sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); 
                sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); 
                sb.append("="); 
                break; 
            } 
            b3 = data[i++] & 0xff; 
            sb.append(base64EncodeChars[b1 >>> 2]); 
            sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); 
            sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); 
            sb.append(base64EncodeChars[b3 & 0x3f]); 
        } 
        return sb.toString(); 
    }
}

  

[] Code to achieve real-time query

  1. Modify the official demo code, I isolated a courier in this class, improve the applicability

  2. The introduction of the class file, and test examples of

Guess you like

Origin www.cnblogs.com/kdn2019/p/11491799.html