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的代码
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 }
------------------------------------------------------------------------------
public static String getResponseBodyAsStringByPost(String url, RequestEntity requestEntity, Map<String, String> headerMap){ return getResponseBodyAsStringByPost(url,requestEntity,1, headerMap); }
这个也需要加上。否则参数不匹配。
这个是和小米公司对接,工程师最后主动提供给我的,这才叫思考到位。