SunnyFarm/tmp/ExpressQueryTest.java
superlishunqin 364b7acbb7 END
2025-10-10 23:22:52 +08:00

107 lines
4.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/**
* 阿里云物流API测试
* 测试快递单号434815453222560韵达快递
*/
public class ExpressQueryTest {
public static void main(String[] args) {
String host = "https://wuliu.market.alicloudapi.com";
String path = "/kdi";
String appcode = "e7b5746ef8854cf2a6da24342ab53af6";
String no = "434815453222560"; // 韵达快递单号
String type = "yunda"; // 韵达快递代码
String urlSend = host + path + "?no=" + no + "&type=" + type;
System.out.println("========================================");
System.out.println("阿里云物流API测试");
System.out.println("========================================");
System.out.println("请求地址: " + urlSend);
System.out.println("快递公司: 韵达快递");
System.out.println("快递单号: " + no);
System.out.println("----------------------------------------");
try {
URL url = new URL(urlSend);
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);
int httpCode = httpURLCon.getResponseCode();
System.out.println("HTTP状态码: " + httpCode);
System.out.println("----------------------------------------");
if (httpCode == 200) {
String json = read(httpURLCon.getInputStream());
System.out.println("✅ API调用成功");
System.out.println("返回结果:");
System.out.println(json);
System.out.println("========================================");
} else {
Map<String, List<String>> map = httpURLCon.getHeaderFields();
List<String> errorList = map.get("X-Ca-Error-Message");
String error = errorList != null && !errorList.isEmpty() ? errorList.get(0) : "未知错误";
System.out.println("❌ API调用失败");
System.out.println("错误信息: " + error);
if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
System.out.println("原因: AppCode错误");
} else if (httpCode == 400 && error.equals("Invalid Url")) {
System.out.println("原因: 请求的 Method、Path 或者环境错误");
} else if (httpCode == 400 && error.equals("Invalid Param Location")) {
System.out.println("原因: 参数错误");
} else if (httpCode == 403 && error.equals("Unauthorized")) {
System.out.println("原因: 服务未被授权或URL和Path不正确");
} else if (httpCode == 403 && error.equals("Quota Exhausted")) {
System.out.println("原因: 套餐包次数用完");
} else if (httpCode == 403 && error.equals("Api Market Subscription quota exhausted")) {
System.out.println("原因: 套餐包次数用完,请续购套餐");
} else {
System.out.println("原因: 参数名错误或其他错误");
}
// 打印所有响应头帮助调试
System.out.println("\n所有响应头:");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("========================================");
}
} catch (MalformedURLException e) {
System.out.println("❌ URL格式错误");
e.printStackTrace();
} catch (UnknownHostException e) {
System.out.println("❌ URL地址错误");
e.printStackTrace();
} catch (Exception e) {
System.out.println("❌ 发生异常");
e.printStackTrace();
}
}
/**
* 读取返回结果
*/
private static String read(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
}