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 paramMap = new HashMap();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 headerMap = new HashMap();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的代码

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

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

文章插图
------------------------------------------------------------------------------
public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity, Map headerMap){return getResponseBodyAsStringByPost(url,requestEntity,1, headerMap);}
这个也需要加上 。否则参数不匹配 。
【java对接申通下单接口示例代码】这个是和小米公司对接 , 工程师最后主动提供给我的 , 这才叫思考到位 。