java
复制代码
package com.cqdh.group.applet;
import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
/**
* @Description 顺丰API测试
* @Date 2023/10/17 16:21
* @Author 余乐
**/
public class SFApiTest {
/**
* 客户编码
*/
private static final String partnerID = "ZYHNYK*****";
/**
* 测试环境url
*/
private static final String url = "https://sfapi-sbox.sf-express.com/std/service";
/**
* 校验码
*/
private static final String checkWord = "pUVFxVbrao35vtzY***********";
/**
* 创建物流订单 ,修改订单orderId即可反复下单
*
* @throws UnsupportedEncodingException
*/
@Test
public void createLogisticsOrder() throws UnsupportedEncodingException {
// 请求参数
String serviceCode = "EXP_RECE_CREATE_ORDER";
String requestID = UUID.randomUUID().toString();
String timespase = System.currentTimeMillis() + "";
String msgData = "{\"expressTypeId\":1,\"orderId\":\"SC1714182733078591144\",\"payMethod\":1,\"totalWeight\":1,\"monthlyCard\":\"7551234567\",\"signBackRemark\":\"test--签\",\"contactInfoList\":[{\"country\":\"CN\",\"contactType\":1,\"tel\":\"13866666666\",\"address\":\"重庆市渝北区两港大道与婵衣路交叉路口往东约140米\",\"contact\":\"小红\"},{\"country\":\"CN\",\"contactType\":2,\"tel\":\"13866666666\",\"address\":\"遵义市汇川区红河路莲花山公园西南侧约150米\",\"contact\":\"张三\"}],\"isDocall\":1}";
String msgDigest = getMsgDigest(msgData, timespase, checkWord);
// 构建请求头
HttpRequest request = HttpRequest.post(url);
request.header("Content-Type", "application/x-www-form-urlencoded");
// 构建请求体
String formData = String.format("serviceCode=%s&partnerID=%s&requestID=%s×tamp=%s&msgDigest=%s&msgData=%s",
serviceCode, partnerID, requestID, timespase, msgDigest, msgData);
// 发送请求并获取响应
HttpResponse response = request.body(formData).execute();
// 解析响应内容
String result = response.body();
System.out.println("响应内容:" + result);
}
/**
* 取消物流订单 ,必须取消成功三次才可上线,通过接口创建成功的订单才可取消
*
* @throws UnsupportedEncodingException
*/
@Test
public void cannelLogisticsOrder() throws UnsupportedEncodingException {
// 请求参数
String serviceCode = "EXP_RECE_UPDATE_ORDER";
String requestID = UUID.randomUUID().toString();
String timespase = System.currentTimeMillis() + "";
String msgData = "{\"dealType\":2,\"language\":\"zh-CN\",\"orderId\":\"SC1714182733078597644\",\"totalWeight\":1,\"waybillNoInfoList\":[]}";
String msgDigest = getMsgDigest(msgData, timespase, checkWord);
// 构建请求头
HttpRequest request = HttpRequest.post(url);
request.header("Content-Type", "application/x-www-form-urlencoded");
// 构建请求体
String formData = String.format("serviceCode=%s&partnerID=%s&requestID=%s×tamp=%s&msgDigest=%s&msgData=%s",
serviceCode, partnerID, requestID, timespase, msgDigest, msgData);
// 发送请求并获取响应
HttpResponse response = request.body(formData).execute();
// 解析响应内容
String result = response.body();
System.out.println("响应内容:" + result);
}
public static String getMsgDigest(String msgData, String timeStamp, String md5Key) throws UnsupportedEncodingException {
return (new Base64()).encodeAsString(md5Encrypt(URLEncoder.encode(msgData + timeStamp + md5Key, "UTF-8")));
}
private static byte[] md5Encrypt(String encryptStr) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(encryptStr.getBytes("utf8"));
return md5.digest();
} catch (Exception var2) {
throw new RuntimeException(var2);
}
}
}
java
复制代码
{
"apiErrorMsg":"",
"apiResponseID":"00018BB37AD8993FDF02951B4A6F363F",
"apiResultCode":"A1000",
"apiResultData":"{\"success\":true,\"errorCode\":\"S0000\",\"errorMsg\":null,\"msgData\":{\"orderId\":\"SC1714182733078591144\",\"originCode\":\"023\",\"destCode\":\"052\",\"filterResult\":2,\"remark\":\"\",\"url\":null,\"paymentLink\":null,\"isUpstairs\":null,\"isSpecialWarehouseService\":null,\"mappingMark\":null,\"agentMailno\":null,\"returnExtraInfoList\":null,\"waybillNoInfoList\":[{\"waybillType\":1,\"waybillNo\":\"SF7444473317974\",\"boxNo\":null,\"length\":null,\"width\":null,\"height\":null,\"weight\":null,\"volume\":null}],\"routeLabelInfo\":[{\"code\":\"1000\",\"routeLabelData\":{\"waybillNo\":\"SF7444473317974\",\"sourceTransferCode\":\"023W\",\"sourceCityCode\":\"023\",\"sourceDeptCode\":\"023\",\"sourceTeamCode\":\"\",\"destCityCode\":\"052\",\"destDeptCode\":\"052GC\",\"destDeptCodeMapping\":\"\",\"destTeamCode\":\"004\",\"destTeamCodeMapping\":\"\",\"destTransferCode\":\"052\",\"destRouteLabel\":\"052GC-004\",\"proName\":\"\",\"cargoTypeCode\":\"C201\",\"limitTypeCode\":\"T4\",\"expressTypeCode\":\"B1\",\"codingMapping\":\"C15\",\"codingMappingOut\":\"\",\"xbFlag\":\"0\",\"printFlag\":\"000000000\",\"twoDimensionCode\":\"MMM={'k1':'052','k2':'052GC','k3':'004','k4':'T4','k5':'SF7444473317974','k6':'','k7':'1d8ee46c'}\",\"proCode\":\"特快\",\"printIcon\":\"00000000\",\"abFlag\":\"\",\"destPortCode\":\"\",\"destCountry\":\"\",\"destPostCode\":\"\",\"goodsValueTotal\":\"\",\"currencySymbol\":\"\",\"cusBatch\":\"\",\"goodsNumber\":\"\",\"errMsg\":\"\",\"checkCode\":\"1d8ee46c\",\"proIcon\":\"\",\"fileIcon\":\"\",\"fbaIcon\":\"\",\"icsmIcon\":\"\",\"destGisDeptCode\":\"052GC\",\"newIcon\":null,\"sendAreaCode\":null,\"destinationStationCode\":null,\"sxLabelDestCode\":null,\"sxDestTransferCode\":null,\"sxCompany\":null,\"newAbFlag\":null,\"destAddrKeyWord\":\"\",\"rongType\":null,\"waybillIconList\":null},\"message\":\"SF7444473317974:\"}],\"contactInfoList\":null,\"sendStartTm\":null,\"customerRights\":null,\"expressTypeId\":null}}"
}