圓通電子面單接口對接

最近有個項目需要與圓通電子面單接口對接。

首先查看了官方文檔API: http://open.yto.net.cn/openplatform/doc

接口使用的是XML格式的數據。

首先說一下xml數據格式

因爲之前一直是使用的json數據格式,結合一些文章來看,決定使用com.thoughtworks.xstream,


    //轉化爲xml
    private String changeToXml(SendVo param){
        XStream xs = new XStream();
        xs.alias("RequestOrder",SendVo.class);
        xs.alias("item", Item.class);
        xs.alias("sender",Sender.class);
        xs.alias("receiver",Sender.class);
        String result = xs.toXML(param).replaceAll("\\s|\r|\t|\n","");
        return result;
    }
便可以直接把bean更改爲xml數據



這裏有些要注意的地方!經過md5(16位)和base64後的內容就爲 LghTkEmsD2tbQ3fsIBRcBg==。

最開始的時候使用了MD5加密但是結果始終不是他給出的栗子。

後來看到一篇文章:https://blog.csdn.net/wdd668/article/details/78987247    

    public static String YuanTong(String sourceStr) {
        String result = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest((sourceStr).getBytes(Charset.forName("utf-8")));
            result = new String(Base64.encodeBase64(bytes),"utf-8");
//            result = Base64.encode(bytes);

        } catch (Exception e) {
            e.printStackTrace();
//            logger.info("圓通生成簽名出錯:{}", e.getMessage());
        }
        return result;
    }


解決了數據轉化問題。接下來又迎來了訪問圓通接口的問題················

他的官網上直說了使用http、post訪問。我便用了

CloseableHttpClient httpclient 
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse httpResponse = httpclient.execute(httppost);


然而。返回的卻是。。500

後來在網上偶然找到這篇文章:http://yuncode.net/code/c_57e0eb4e1ef1614

採用瞭如下方法

/**
	 * 向指定 URL 發送POST方法的請求
	 * @since 2018.05請求圓通數據引入.
	 * @param url 發送請求的 URL
	 * @param params 請求的參數集合
	 * @return 遠程資源的響應結果
	 */
	public static 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());
				}
				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();
	}


----額暫時記下等會來研究爲什麼不可以使用上面的方法訪問
----額暫時記下等會來研究爲什麼不可以使用上面的方法訪問

所有東西都處理完畢了···················

今天導了一些數據,,,悲劇了 (5.6W條數據吧)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章