java对接申通下单接口示例代码
上面是控制台示例代码
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的代码
public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity,int retryCount, Map<String, String> headerMap){
StringBuffer buffer = new StringBuffer(url);
PostMethod method = new PostMethod(buffer.toString());
if(headerMap!=null){
for(Entry<String, String> entry:headerMap.entrySet()){
method.addRequestHeader(entry.getKey(), entry.getValue());
}
}
if(requestEntity!=null){
method.setRequestEntity(requestEntity);
}
do{
try {
try {
int code = getHttpClient().executeMethod(method);
if (code == HttpStatus.SC_OK) {
return method.getResponseBodyAsString();
} else {
LOGGER.warn("http failed, code {}, response {}, url {}", code, method.getResponseBodyAsString(), url);
}
} finally {
method.releaseConnection();
}
} catch (Throwable e) {
LOGGER.warn("http error, url:"+ url, e);
}
}while(--retryCount>0);
return null;
}
------------------------------------------------------------------------------
public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity, Map<String, String> headerMap){
return getResponseBodyAsStringByPost(url,requestEntity,1, headerMap);
}
这个也需要加上。否则参数不匹配。
这个是和小米公司对接,工程师最后主动提供给我的,这才叫思考到位。
java对接申通下单接口示例代码的更多相关文章
- wechat开发笔记之1.接口示例代码
修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...
- EasyNVR网页Chrome无插件播放摄像机视频功能二次开发之云台控制接口示例代码
随着多媒体技术和网络通信技术的迅速发展,视频监控技术在电力系统.电信行业.工业监控.工地.城市交通.水利系统.社区安防等领域得到越来越广泛的应用.摄像头直播视频监控通过网络直接连接,可达到的世界任何角 ...
- Java 利用 UUID 生成唯一性 ID 示例代码
用户ID首先生成,订单ID的生成可依赖用户ID. 下面代码前六位是日期,后八位是随机数,用于生成用户ID. public String getNewUserId() { String ipAddres ...
- Java 超类引用子类对象的示例代码
动态方法分配 dynamic method dispatch 一个被重写的方法的调用会在运行时解析,而不是编译时解析 Java 会根据在调用发生时引用的对象的类型来判断所要执行的方法 public c ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用
文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...
- JAVA的免费天气api接口调用示例
step1:选择本文所示例的接口"免费天气api" url:https://www.juhe.cn/docs/api/id/39/aid/87 step2:每个接口都需要传入一个参 ...
- java接口对接——别人调用我们接口获取数据
java接口对接——别人调用我们接口获取数据,我们需要在我们系统中开发几个接口,给对方接口规范文档,包括访问我们的接口地址,以及入参名称和格式,还有我们的返回的状态的情况, 接口代码: package ...
- Java内部类超详细总结(含代码示例)
什么是内部类 什么是内部类? 顾名思义,就是将一个类的定义放在另一个类的内部. 概念很清楚,感觉很简单,其实关键在于这个内部类放置的位置,可以是一个类的作用域范围.一个方法的或是一个代码块的作用域范围 ...
随机推荐
- [Swift]LeetCode38. 报数 | Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [Swift]LeetCode628. 三个数的最大乘积 | Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- Elasticsearch基础知识分享
1. Elasticsearch背景介绍 Elasticsearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elast ...
- Python 实现获取微信好友信息
最近用闲余时间看了点python,在网上冲浪时发现有不少获取微信好友信息的博客,对此比较感兴趣,于是自己敲了敲顺便记录下来. 一.使用 wxpy 模块库获取好友男比例信息和城市分布. # -*- co ...
- vue组件如何被其他项目引用
自己写的vue组件怎么才能让其他人引用呢,或者是共用组件如何让其他项目引用.本文就粗细的介绍下,如有疑问欢迎共同讨论.在这里你能了解下如下知识点: 1. 如何发布一个包到npmjs仓库上 2.如何引用 ...
- H5本地存储sessionStorage和localStorage的区别
sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁.因此sessionStorage不是一种持久化的本地 ...
- System.Data.Entity.Infrastructure.DbUpdateException
异常描述: 捕捉到 System.Data.Entity.Infrastructure.DbUpdateException HResult=-2146233087 Message=无法更新 E ...
- 那些令人惊艳的TensorFlow扩展包和社区贡献模型
随着TensorFlow发布的,还有一个models库(仓库地址:https://github.com/tensorflow/models),里面包含官方及社群所发布的一些基于TensorFlow实现 ...
- C#3.0导航
C#3.0主要特性 智能的编译器 编译器,背后的默默付出者 Lamdba表达式与表达式树 匿名方法的革命 扩展方法 优雅的对类进扩展 (待完成) LINQ 还有这种操作? (待完成)