java免费获取物流信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Gavin_wangzg/article/details/79701113

原理其实很简单就是去java自带的HttpURLConnection模拟浏览器请求快递搜索的方法

第一步:分析快递搜索框是怎么来查询的,在web页面上,无非就是用Jquery的ajax动态的去后台读取物流信息
这里写图片描述
如上图,分析它的url是如何拼接的,显然只有三个参数,type,postid,temp
type:对应物流公司的code(后面在具体讲)
postid:就是我们所查询的物流单号
temp:很简单,Math.random()生成的一个随机数
知道了这些后,接下来就好办了

第二步:该JAVA登场了

public class LogisticsUtil {
    public static final String QUERYURL = "http://www.kuaidi100.com/query?";

    public static String setUrl(String logisticsCode, String logisticsNo) {
        String temp = String.valueOf(Math.random());
        StringBuilder sb = new StringBuilder(QUERYURL);
        sb.append("tpye=").append(logisticsCode).append("&");
        sb.append("postid=").append(logisticsNo).append("&");
        sb.append("temp=").append(temp);
        return sb.toString();
    }

    public static String queryData(String logisticsCode, String logisticsNo) {
        String line = "";
        String temp = String.valueOf(Math.random());
        String url = "http://www.kuaidi100.com/query?type=" + logisticsCode + "&postid=" + logisticsNo + "&temp=" + temp + "";
        try {
            URL realURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realURL.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
            conn.connect();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                while ((line = reader.readLine()) != null) {
                    return line;
                }
                reader.close();
                conn.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return line;

    }

    public static void main(String[] args) throws HttpException, IOException {
        System.out.println(queryData("ems", "EL#########HK"));//
    }

}

通过queryData的方法就物流公司的code和快递单号传入,并可从响应体中读出物流信息的字节流解析出来就是如下图的JSON格式数据
这里写图片描述
好了,拿的这些信息后在好好发挥它们的作用。

第三步,之前所前的type,对应物流公司的code其实是每个服务提供商自己定义的一套,我这就选取了其中的几个

public enum LogisticsCompany {
    SF("顺丰", 1, "shunfeng"),
    ZT("中通", 2, "zhongtong"),
    ST("申通", 3, "shentong"),
    YT("圆通", 4, "yuantong"),
    HT("汇通", 5, "huitongkuaidi"),
    YD("韵达", 6, "yunda"),
    YZ("邮政包裹/平邮", 7, "youzhengguonei"),
    EMS("EMS", 8, "ems"),
    TT("天天", 9, "tiantian"),
    DB("德邦", 10, "debangwuliu");

    private String name;
    private int value;
    private String type;

其中的type也就是对应queryData方法中的logisticsCode参数。
大多的物流查询第三方都类似的情况,这样连注册都省了。

猜你喜欢

转载自blog.csdn.net/gavin_wangzg/article/details/79701113
今日推荐