package com.sinata.core.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.sinata.core.base.tips.ErrorTip;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
import java.net.UnknownHostException;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 物流快递接口
|
* https://market.aliyun.com/products/57126001/cmapi021863.html?spm=5176.2020520132.101.2.7e567218tcHFxx#sku=yuncode1586300000
|
*
|
* @author goku
|
* @date 2023/3/19
|
*/
|
public class ExpressApi {
|
|
public static void main(String[] args) {
|
Object query = query("773079237697693", null);
|
System.out.println(JSON.toJSONString(query));
|
}
|
|
/**
|
* 全国快递物流查询
|
*
|
* @param no 快递单号 【顺丰请输入单号 : 收件人或寄件人手机号后四位。例如:123456789:1234】
|
* @param type 快递公司字母简写:不知道可不填 95%能自动识别,填写查询速度会更快【见产品详情】
|
*/
|
public static Object query(String no, String type) {
|
// 【1】请求地址 支持http 和 https 及 WEBSOCKET
|
String host = "https://wuliu.market.alicloudapi.com";
|
// 【2】后缀
|
String path = "/kdi";
|
// 【3】开通服务后 买家中心-查看AppCode
|
String appcode = "98c01d788f374355a00b9367e0e488f5";
|
// 【5】拼接请求链接
|
String urlSend = host + path + "?no=" + no + "&type=" + (type == null ? "" : type);
|
try {
|
URL url = new URL(urlSend);
|
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
|
// 格式Authorization:APPCODE (中间是英文空格)
|
httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);
|
int httpCode = httpURLCon.getResponseCode();
|
if (httpCode == 200) {
|
String json = read(httpURLCon.getInputStream());
|
return JSON.parseObject(json, Map.class);
|
} else {
|
Map<String, List<String>> map = httpURLCon.getHeaderFields();
|
String error = map.get("X-Ca-Error-Message").get(0);
|
if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
|
return new ErrorTip(500, "AppCode错误");
|
} else if (httpCode == 400 && error.equals("Invalid Url")) {
|
return new ErrorTip(500, "请求的 Method、Path 或者环境错误");
|
} else if (httpCode == 400 && error.equals("请求的 Method、Path 或者环境错误")) {
|
return new ErrorTip(500, "参数错误");
|
} else if (httpCode == 403 && error.equals("Unauthorized")) {
|
return new ErrorTip(500, "服务未被授权(或URL和Path不正确)");
|
} else if (httpCode == 403 && error.equals("Quota Exhausted")) {
|
return new ErrorTip(500, "套餐包次数用完");
|
} else {
|
return new ErrorTip(500, "参数名错误 或 其他错误");
|
}
|
}
|
} catch (MalformedURLException e) {
|
return new ErrorTip(500, "URL格式错误");
|
} catch (UnknownHostException e) {
|
return new ErrorTip(500, "URL地址错误");
|
} catch (Exception e) {
|
// 打开注释查看详细报错异常信息
|
// e.printStackTrace();
|
}
|
return new ErrorTip(500, "查询失败");
|
}
|
|
/**
|
* 读取返回结果
|
*/
|
private static String read(InputStream is) throws IOException {
|
StringBuffer sb = new StringBuffer();
|
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
String line = null;
|
while ((line = br.readLine()) != null) {
|
line = new String(line.getBytes(), "utf-8");
|
sb.append(line);
|
}
|
br.close();
|
return sb.toString();
|
}
|
|
}
|