:question:如何對(duì)請(qǐng)求順豐API


分析結(jié)果:
用戶(hù)注冊(cè)成為 CP得到 partnerID ,傳 serviceCode 來(lái)指定的調(diào)用的 API服務(wù)代碼,按指定的加密方式得到 數(shù)字簽名 ,再把 相關(guān)請(qǐng)求的報(bào)文、時(shí)間戳、UUID隨機(jī)數(shù) 作為參數(shù),請(qǐng)求結(jié)果。
:newspaper:請(qǐng)求報(bào)文分析
每個(gè)請(qǐng)求的報(bào)文有所不同
這里以 路由查詢(xún)報(bào)文 來(lái)分析
報(bào)文元素

報(bào)文示例
{
"language": "0",
"trackingType": "1",
"trackingNumber": ["444003077898", "441003077850"],
"methodType": "1"
}
復(fù)制代碼
按照相關(guān)格式填寫(xiě)業(yè)務(wù)中的需要查詢(xún)的快遞訂單id進(jìn)去,實(shí)現(xiàn)相關(guān)查詢(xún)
:chestnut:Demo實(shí)踐
Demo 代碼

目標(biāo)1:PostMan發(fā)送路由查詢(xún)請(qǐng)求
目標(biāo):使用 POSTMAN發(fā)送一個(gè)路由查詢(xún)請(qǐng)求
??填參準(zhǔn)備
?注意設(shè)置請(qǐng)求頭


??最重要的數(shù)字簽名
文檔中有加密demo
修改后輸出需要的參數(shù)
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
// 請(qǐng)求唯一號(hào)
String requestID = UUID.randomUUID().toString().replace("-", "");
//時(shí)間戳 取報(bào)文中的timestamp(調(diào)用接口時(shí)間戳)
String timestamp = String.valueOf(System.currentTimeMillis());
//客戶(hù)校驗(yàn)碼 使用順豐分配的客戶(hù)校驗(yàn)碼
String checkword = "XXXXXXX";
//業(yè)務(wù)報(bào)文 去報(bào)文中的msgData(業(yè)務(wù)數(shù)據(jù)報(bào)文)
String msgData = "{n"language": "0", "trackingType": "1", "trackingNumber": ["444003077898", "441003077850"], "methodType": "1" }";
//將業(yè)務(wù)報(bào)文+時(shí)間戳+校驗(yàn)碼組合成需加密的字符串(注意順序)
String toVerifyText = msgData+timestamp+checkWord;
//因業(yè)務(wù)報(bào)文中可能包含加號(hào)、空格等特殊字符,需要urlEnCode處理
toVerifyText = URLEncoder.encode(toVerifyText,"UTF-8");
//進(jìn)行Md5加密
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(toVerifyText.getBytes("UTF-8"));
byte[] md = md5.digest();
//通過(guò)BASE64生成數(shù)字簽名
String msgDigest = new String(new BASE64Encoder().encode(md));
System.out.println("時(shí)間戳:"+timestamp);
System.out.println("請(qǐng)求唯一號(hào):"+requestID);
System.out.println("加密后的數(shù)字簽名:"+msgDigest);
}
復(fù)制代碼
運(yùn)行得到相關(guān)參數(shù):

再填入剩下的參數(shù)

請(qǐng)求成功
目標(biāo)2:JAVA請(qǐng)求路由查詢(xún) API
目標(biāo):用 Java實(shí)現(xiàn) post請(qǐng)求路由查詢(xún)
就是從使用PostMan發(fā)送請(qǐng)求到在 Java中使用 HTTP請(qǐng)求工具類(lèi),對(duì)服務(wù)器傳參,發(fā)送請(qǐng)求
添加HTTP請(qǐng)求工具類(lèi)請(qǐng)求接口
import org.Apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
public static String postSFAPI(String url, String xml, String verifyCode) {
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("xml", xml));
parameters.add(new BasicNameValuePair("verifyCode", verifyCode));
HttpPost post = postForm(url, new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8));
String body = "";
body = invoke(httpClient, post);
try {
httpClient.close();
} catch (IOException var9) {
logger.error("HttpClientService post error", var9);
}
return body;
}
private static String invoke(CloseableHttpClient httpclient, HttpUriRequest httpost) {
HttpResponse response = sendRequest(httpclient, httpost);
String body = "";
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
body = parseResponse(response);
}
return body;
}
private static String parseResponse(HttpResponse response) {
HttpEntity entity = response.getEntity();
String body = "";
try {
if (entity != null) {
body = EntityUtils.toString(entity);
}
} catch (ParseException | IOException var4) {
logger.error("HttpClientService paseResponse error", var4);
}
return body;
}
private static HttpResponse sendRequest(CloseableHttpClient httpclient, HttpUriRequest httpost) {
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpost);
} catch (IOException var4) {
logger.error("HttpClientService sendRequest error", var4);
}
return response;
}
private static HttpPost postForm(String url, StringEntity entity) {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
return httpPost;
}
public static String post(String url, Map<String, String> params) throws UnsupportedEncodingException {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(60000).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-Type", "Application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> paramsList = new ArrayList<>();
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
for (Map.Entry<String, String> entry : params.entrySet()) {
paramsList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramsList, "UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
String body = invoke(httpClient, httpPost);
try {
httpClient.close();
} catch (IOException var9) {
logger.error("HttpClientService post error", var9);
}
return body;
}
}
復(fù)制代碼
調(diào)用工具類(lèi)請(qǐng)求API 添加代碼
Map<String,String> params = new HashMap<>();
params.put("requestID",requestID);
params.put("msgDigest",msgDigest);
params.put("msgData",msgData);
params.put("timestamp",timestamp);
params.put("partnerID","xxxxxx");
params.put("serviceCode","EXP_RECE_SEARCH_ROUTES");
String result = null;
try {
result = HttpClientUtil.post("https://sfapi-sbox.sf-express.com/std/service", params);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(result);
復(fù)制代碼
請(qǐng)求成功

目標(biāo)3:舉一反三
猜想:
只改變請(qǐng)求的服務(wù)代碼和請(qǐng)求的報(bào)表亦可成功訪問(wèn)對(duì)應(yīng)的服務(wù)?
前提條件
接入其他 API時(shí),需要先申請(qǐng)權(quán)限

修改請(qǐng)求參數(shù)
- serviceCode服務(wù)代碼
- msgData請(qǐng)求報(bào)表
建議抽取靜態(tài)變量,方便修改
下面試著請(qǐng)求 下訂單 接口
// 現(xiàn)在指定 API的服務(wù)名
private static String SERVICE_CODE = "EXP_RECE_CREATE_ORDER";
// 請(qǐng)求報(bào)文
private static String msgData = "{
"cargoDetails":[
{
"count":2.365,
"unit":"個(gè)",
"weight":6.1,
"amount":100.5111,
"currency":"HKD",
"name":"護(hù)膚品1",
"sourceArea":"CHN"
}],
"contactInfoList": [
{
"address":"廣東省深圳市南山區(qū)軟件產(chǎn)業(yè)基地11棟",
"contact":"小曾",
"contactType":1,
"country":"CN",
"postCode":"580058",
"tel":"4006789888"
},
{
"address":"廣東省廣州市白云區(qū)湖北大廈",
"company":"順豐速運(yùn)",
"contact":"小邱",
"contactType":2,
"country":"CN",
"postCode":"580058",
"tel":"18688806057"
}],
"language":"zh_CN",
"orderId":"OrderNum20200612223"
}";
復(fù)制代碼
直接運(yùn)行

測(cè)試成功
:statue_of_liberty:總結(jié)
我們嘗試了 使用 postman對(duì)路由查詢(xún) API發(fā)出請(qǐng)求,
Java使用 HTTP工具類(lèi)來(lái)接入路由查詢(xún) API,
同時(shí)舉一反三地接入了生成訂單 API
當(dāng)然 Demo還可以進(jìn)一步進(jìn)行完善
Demo 完善展望
- 封裝報(bào)文
- 抽取服務(wù)代碼做枚舉類(lèi)
- 聲明用戶(hù)屬性的常量