java对接申通下单接口示例代码

编程入门 行业动态 更新时间:2024-10-09 16:27:35

java对接申通下单接口<a href=https://www.elefans.com/category/jswz/34/1770116.html style=示例代码"/>

java对接申通下单接口示例代码

上面是控制台示例代码

public class Sample{private final static String URL = ":8001/api/Order/ProcessRequestTest";private final static String APPKEY = "appkey";private final static String APPSECRET = "secret";/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {Date date = new Date();String dateStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");Map<String, String> paramMap = new HashMap<String, String>();paramMap.put("RequestType", "json");paramMap.put("Func", "sto.order.add");JSONObject contentObj = new JSONObject();contentObj.put("SenderName", "张三");contentObj.put("SenderPhone", "15111111111");contentObj.put("SenderMobile", "15111111111");contentObj.put("SenderProvince", "上海");contentObj.put("SenderCity", "上海市");contentObj.put("SenderDistrict", "青浦区");contentObj.put("SenderAddress", "青浦区");contentObj.put("ReceiverName", "李四");contentObj.put("ReceiverMobile", "15111111112");contentObj.put("ReceiverProvince", "上海");contentObj.put("ReceiverCity", "上海市");contentObj.put("ReceiverDistrict", "青浦区");contentObj.put("ReceiverAddress", "青浦区");contentObj.put("GoodsType", "物品");String contentStr =Base64.encodeBase64String(contentObj.toString().getBytes()).replaceAll("\r\n", "");System.out.println("contentStr:" + contentStr);System.out.println("Decode:" + new String(Base64.decodeBase64(contentStr)));paramMap.put("Content", contentStr);paramMap.put("AppKey", APPKEY);paramMap.put("TimeSpan", dateStr);paramMap.put("Signature", DigestUtils.md5Hex(APPKEY+ dateStr + contentStr + APPSECRET));paramMap.put("Version", "1.0");System.out.println(JSONObject.fromObject(paramMap));StringRequestEntity entity = null;try {entity = new StringRequestEntity(JSONObject.fromObject(paramMap).toString(), "application/json", "UTF-8");} catch (Exception e) {e.printStackTrace();}Map<String, String> headerMap = new HashMap<String, String>();headerMap.put("Connection", "Keep-Alive");headerMap.put("Cache-Control", "no-cache");headerMap.put("Accept", "text/html,application/xhtml+xml,application/xml,application/json;");String responseStr = HttpClientUtils.getResponseBodyAsStringByPost(URL, entity, headerMap);System.out.println(responseStr);}
}

  下面的代码是post的代码

 1     public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity,int retryCount, Map<String, String> headerMap){
 2         StringBuffer buffer = new StringBuffer(url);
 3         PostMethod method = new PostMethod(buffer.toString());
 4         if(headerMap!=null){
 5             for(Entry<String, String> entry:headerMap.entrySet()){
 6                 method.addRequestHeader(entry.getKey(), entry.getValue());
 7             }
 8         }
 9         if(requestEntity!=null){
10             method.setRequestEntity(requestEntity);
11         }
12         do{
13             try {
14                 try {
15                     int code = getHttpClient().executeMethod(method);
16                     if (code == HttpStatus.SC_OK) {
17                         return method.getResponseBodyAsString();
18                     } else {
19                         LOGGER.warn("http failed, code {}, response {}, url {}", code, method.getResponseBodyAsString(), url);
20                     }
21                 } finally {
22                     method.releaseConnection();
23                 }
24             } catch (Throwable e) {
25                 LOGGER.warn("http error, url:"+ url, e);
26             }
27         }while(--retryCount>0);
28         return null;
29     }
View Code

 ------------------------------------------------------------------------------

public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity, Map<String, String> headerMap){return getResponseBodyAsStringByPost(url,requestEntity,1, headerMap);}

  这个也需要加上。否则参数不匹配。

  这个是和小米公司对接,工程师最后主动提供给我的,这才叫思考到位。

 

更多推荐

java对接申通下单接口示例代码

本文发布于:2024-02-28 02:05:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   下单   接口   代码   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!