申通对接成功
This commit is contained in:
@@ -14,13 +14,14 @@ import java.net.URL;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
public class StoRouteQueryUtil {
|
public class StoRouteQueryUtil {
|
||||||
// 请填写你自己的key/secret
|
// 请填写你自己的key/secret
|
||||||
private static final String APP_KEY = "CAKWRqzGkVRVVFj";
|
private static final String APP_KEY = "CAKDSqybdwbzlUC";
|
||||||
private static final String APP_SECRET = "";
|
private static final String APP_SECRET = "wGRMVXwIf8sp4m4W6tWJcOaDfY7EPblQ";
|
||||||
private static final String FROM_CODE = ""; // 你的from_code
|
private static final String FROM_CODE = "CAKDSqybdwbzlUC"; // 你的from_code
|
||||||
private static final String API_URL = "http://cloudinter-linkgatewaytest.sto.cn/gateway/link.do";
|
private static final String API_URL = "https://cloudinter-linkgateway.sto.cn/gateway/link.do";
|
||||||
|
|
||||||
// 申通签名算法 body+appSecret,MD5后Base64
|
// 申通签名算法 body+appSecret,MD5后Base64
|
||||||
public static String sign(String body, String secret) {
|
public static String sign(String body, String secret) {
|
||||||
@@ -44,26 +45,27 @@ public class StoRouteQueryUtil {
|
|||||||
content.put("waybillNoList", waybillNoList);
|
content.put("waybillNoList", waybillNoList);
|
||||||
String contentStr = content.toJSONString();
|
String contentStr = content.toJSONString();
|
||||||
|
|
||||||
// 构造请求参数
|
// 构造表单参数字符串
|
||||||
JSONObject params = new JSONObject();
|
StringBuilder form = new StringBuilder();
|
||||||
params.put("api_name", "STO_TRACE_QUERY_COMMON");
|
form.append("api_name=STO_TRACE_QUERY_COMMON");
|
||||||
params.put("content", contentStr);
|
form.append("&content=").append(URLEncoder.encode(contentStr, "UTF-8"));
|
||||||
params.put("from_appkey", APP_KEY);
|
form.append("&from_appkey=").append(APP_KEY);
|
||||||
params.put("from_code", FROM_CODE);
|
form.append("&from_code=").append(APP_KEY);
|
||||||
params.put("to_appkey", "sto_trace_query");
|
form.append("&to_appkey=sto_trace_query");
|
||||||
params.put("to_code", "sto_trace_query");
|
form.append("&to_code=sto_trace_query");
|
||||||
params.put("data_digest", sign(contentStr, APP_SECRET));
|
form.append("&data_digest=").append(URLEncoder.encode(sign(contentStr, APP_SECRET), "UTF-8"));
|
||||||
|
|
||||||
|
System.out.println("申通表单请求体:" + form);
|
||||||
|
|
||||||
String jsonParams = params.toJSONString();
|
|
||||||
URL url = new URL(API_URL);
|
URL url = new URL(API_URL);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
conn.setDoInput(true);
|
conn.setDoInput(true);
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
||||||
OutputStream out = conn.getOutputStream();
|
OutputStream out = conn.getOutputStream();
|
||||||
out.write(jsonParams.getBytes(StandardCharsets.UTF_8));
|
out.write(form.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
StringBuilder sbResult = new StringBuilder();
|
StringBuilder sbResult = new StringBuilder();
|
||||||
@@ -89,6 +91,11 @@ public class StoRouteQueryUtil {
|
|||||||
if (result == null || result.trim().isEmpty()) {
|
if (result == null || result.trim().isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// 新增:如果返回是XML,直接打印并返回null
|
||||||
|
if (result.trim().startsWith("<")) {
|
||||||
|
System.err.println("申通返回XML错误:" + result);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
JSONObject json = JSON.parseObject(result);
|
JSONObject json = JSON.parseObject(result);
|
||||||
JSONObject data = json.getJSONObject("data");
|
JSONObject data = json.getJSONObject("data");
|
||||||
@@ -113,7 +120,7 @@ public class StoRouteQueryUtil {
|
|||||||
// main方法测试
|
// main方法测试
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
List<String> waybillNos = new ArrayList<>();
|
List<String> waybillNos = new ArrayList<>();
|
||||||
waybillNos.add("777031922725111"); // 测试单号
|
waybillNos.add("777325378857266"); // 测试单号
|
||||||
String result = queryRoute(waybillNos);
|
String result = queryRoute(waybillNos);
|
||||||
System.out.println("申通原始返回:" + result);
|
System.out.println("申通原始返回:" + result);
|
||||||
OaExpressVo vo = parseData(result);
|
OaExpressVo vo = parseData(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user