圆通电子面单接口对接

最近有个项目需要与圆通电子面单接口对接。

首先查看了官方文档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条数据吧)



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