Java implements logistics query (using Alibaba Cloud logistics query interface)

Express logistics query interface [latest version]_Express query_data API_e-commerce-cloud market-Aliyun (aliyun.com)

First of all, purchase the Alibaba Cloud Express logistics query interface, which is free. If you test it, you can buy it for free, which is very convenient.

 After the purchase is complete, click to enter your console. You can see the parameter data you need:

As well as the interface details provided above, click to copy and use it directly. 

 

The corresponding interface information is available, and it can be used only by putting in the required parameters. 

 JAVA code implementation:

Obtain the codes of all logistics, in fact, you can still find them without passing the codes, 95% can be detected automatically, but in order to ensure 100% accurate detection, it is recommended to pass in the current logistics codes: such as: Zhongtong ---> zto

Included in the code, query the code of the logistics

The logic is as follows: when the consignor finishes delivering the goods, the invoice number and logistics code of the goods should be recorded and stored in the database .

When querying, obtain the current order, the invoice number of the current order, and the logistics code to query detailed logistics details.

import com.alibaba.fastjson2.JSONObject;
import com.furnish.web.wn.vo.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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;

@RestController
@RequestMapping("/wnapp/wl")
@CrossOrigin
public class WuLiuController {

    /**
     * 采用阿里云快递物流查询接口 ================
     * @param
     */
    @GetMapping ("/getWl")
    public String getWl(String no,String phone ,String type) {//物流码【4】请求参数,不知道可不填 95%能自动识别
        
        //传过来电话的后四位
        String host = "https://kdwlcxf.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/kdwlcx";// 【2】后缀
        //TODO 测试用AppCode 后期需更改正式的
        String appcode = "123465"; // 【3】开通服务后 买家中心-查看AppCode
        no = no+":"+phone; // 【4】请求参数,物流单号+电话后四位
        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`")) {
                    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(error);
                }
            }

        } catch (MalformedURLException e) {
            return "URL格式错误";
        } catch (UnknownHostException e) {
            return "URL地址错误";
        } catch (Exception e) {
            // 打开注释查看详细报错异常信息
            // e.printStackTrace();
        }
        return "";
    }

    /**
     * 查询物流编码
     * @param type
     */
    @GetMapping ("/getType")
    @ApiOperation("查询物流的公司编码")
    public String main(String type) {

        String host = "https://kdwlcxf.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/cExpressLists";// 【2】后缀
        //TODO 个人测试用的,后续需更改正式的
        String appcode = "123456"; // 【3】开通服务后 买家中心-查看AppCode

        String urlSend = host + path + "?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`")) {
                    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(error);
                }
            }

        } catch (MalformedURLException e) {
            System.out.println("URL格式错误");
        } catch (UnknownHostException e) {
            System.out.println("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();
    }
}

Guess you like

Origin blog.csdn.net/Java_Mr_Jin/article/details/126318883
Recommended