java 根据单号获取物流信息

文章介绍了如何在Spring框架下创建一个工具类,用于发送HTTPS请求到阿里云WuliuAPI,处理响应状态码和各种错误情况,如AppCode错误、URL错误等。
摘要由CSDN通过智能技术生成

直接套用工具类

import org.springframework.stereotype.Component;
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;

@Component
public class Tools2 {
    private static final String APP_CODE = "xxxxxxxxxx";  //阿里云AppCode
//    public static void main(String[] args) {
    public String LogisticsInformation(String number){
        String host = "https://wuliu.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/kdi";  // 【2】后缀
        String appcode = APP_CODE; // 【3】开通服务后 买家中心-查看AppCode
        String no = number;// 【4】请求参数,详见文档描述
        String type = ""; //  【4】请求参数,不知道可不填 95%能自动识别
        String urlSend = host + path + "?no=" + no +"&type="+type;  // 【5】拼接请求链接
        try {
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            httpURLCon .setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
                String json = read(httpURLCon.getInputStream());
                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json:");
                System.out.print(json);
                return json;
            } 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 "AppCode错误 ";
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    return"请求的 Method、Path 或者环境错误";
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    return"参数错误";
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    return"服务未被授权(或URL和Path不正确)";
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    return"套餐包次数用完 ";
                } else if (httpCode == 403 && error.equals("Api Market Subscription quota exhausted")) {
                    return"套餐包次数用完,请续购套餐";
                } else {
                    return"参数名错误 或 其他错误"+error;
                }
            }
        } catch (MalformedURLException e) {
            return"URL格式错误";
        } catch (UnknownHostException e) {
            return"URL地址错误";
        } catch (Exception e) {
            // 打开注释查看详细报错异常信息
            // e.printStackTrace();
        }
       return "其他错误";
    }

    /*
     * 读取返回结果
     */
    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();
    }
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值