標籤:快遞鳥 電子面單介面
(1)電子面單介面是快遞鳥提供給獨立電商、倉儲管理系統、物流供應鏈等物流系統平台使用的下單介面。
(2)為客戶解決線上發貨需求,商戶通過網路選擇快遞公司發送請求通知快遞公司有快遞要發貨。
(3)客戶把資料通過此介面轉寄到快遞鳥,由快遞鳥為您安排快遞員上門取件的服務。
(4)訂單編號(OrderCode)不可重複提交,重複提交系統會返回具體錯誤碼。
(5)介面支援的訊息接收方式為HTTP POST,要求方法的編碼格式(utf-8):"application/x-www-form-urlencoded;charset=utf-8"。
(6)JAVA對接源碼
import java.io.BufferedReader;import java.io.IOException; import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map;import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;import java.security.MessageDigest; /** * * 快遞鳥電子面單介面 * */ public class KdGoldAPIDemo {//電商IDprivate String EBusinessID="請到快遞鳥官網申請http://www.kdniao.com/ServiceApply.aspx";//電商加密私密金鑰,快遞鳥提供,注意保管,不要泄漏private String AppKey="請到快遞鳥官網申請http://www.kdniao.com/ServiceApply.aspx";//請求url, 正式環境地址:http://api.kdniao.cc/api/Eorderserviceprivate String ReqURL="http://testapi.kdniao.cc:8081/api/Eorderservice";/** * Json方式 電子面單 * @throws Exception */public String orderOnlineByJson() throws Exception{String requestData= "{‘OrderCode‘: ‘012657700387‘," + "‘ShipperCode‘:‘EMS‘," + "‘PayType‘:1," + "‘ExpType‘:1," + "‘Cost‘:1.0," + "‘OtherCost‘:1.0," + "‘Sender‘:" + "{" + "‘Company‘:‘LV‘,‘Name‘:‘Taylor‘,‘Mobile‘:‘15018442396‘,‘ProvinceName‘:‘上海‘,‘CityName‘:‘上海‘,‘ExpAreaName‘:‘青浦區‘,‘Address‘:‘明珠路73號‘}," + "‘Receiver‘:" + "{" + "‘Company‘:‘GCCUI‘,‘Name‘:‘Yann‘,‘Mobile‘:‘15018442396‘,‘ProvinceName‘:‘北京‘,‘CityName‘:‘北京‘,‘ExpAreaName‘:‘朝陽區‘,‘Address‘:‘三裡屯街道雅秀大廈‘}," + "‘Commodity‘:" + "[{" + "‘GoodsName‘:‘鞋子‘,‘Goodsquantity‘:1,‘GoodsWeight‘:1.0}]," + "‘Weight‘:1.0," + "‘Quantity‘:1," + "‘Volume‘:0.0," + "‘Remark‘:‘小心輕放‘," + "‘IsReturnPrintTemplate‘:1}";Map<String, String> params = new HashMap<String, String>();params.put("RequestData", urlEncoder(requestData, "UTF-8"));params.put("EBusinessID", EBusinessID);params.put("RequestType", "1007");String dataSign=encrypt(requestData, AppKey, "UTF-8");params.put("DataSign", urlEncoder(dataSign, "UTF-8"));params.put("DataType", "2");String result=sendPost(ReqURL, params);//根據公司業務處理返回的資訊......return result;}/** * MD5加密 * @param str 內容 * @param charset 編碼方式 * @throws Exception */@SuppressWarnings("unused")private String MD5(String str, String charset) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes(charset)); byte[] result = md.digest(); StringBuffer sb = new StringBuffer(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 = Base64.encode(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簽名產生 * @param content 內容 * @param keyValue Appkey * @param charset 編碼方式 * @throws UnsupportedEncodingException ,Exception * @return DataSign簽名 */@SuppressWarnings("unused")private String encrypt (String content, String keyValue, String charset) throws UnsupportedEncodingException, Exception{if (keyValue != null){return base64(MD5(content + keyValue, charset), charset);}return base64(MD5(content, charset), charset);} /** * 向指定 URL 發送POST方法的請求 * @param url 發送請求的 URL * @param params 請求的參數集合 * @return 遠端資源的響應結果 */@SuppressWarnings("unused")private String sendPost(String url, Map<String, String> params) { OutputStreamWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); try { URL realUrl = new URL(url); HttpURLConnection conn =(HttpURLConnection) realUrl.openConnection(); // 發送POST請求必須設定如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // POST方法 conn.setRequestMethod("POST"); // 設定通用的請求屬性 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("Content-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(); // 定義BufferedReader輸入資料流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } //使用finally塊來關閉輸出資料流、輸入資料流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result.toString(); }}
電商電子面單設定-快遞鳥API介面