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

上面是控制台示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Sample{
 
    private final static String URL = "http://order.sto-express.cn: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
复制代码

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

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

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

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

 

posted @   小菜鸟叽叽喳喳  阅读(546)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示