通过快递100 api 获取物流信息

package com.dongfang.common.utils.wuliu;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONArray;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;




public class Kuaidi100 {

private String get(String url) throws ClientProtocolException, IOException{
        DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
        HttpGet httpGet = new HttpGet(url);
        HttpUriRequest request = null;
        request = httpGet;
        HttpContext localContext = new BasicHttpContext();
        HttpResponse response = httpClient.execute(request, localContext);
        return EntityUtils.toString(response.getEntity());
    }

public List<Map<String, String>> queryLogisticsInfoByKuadi100(String trackingNo) {
        try {
            if(StringUtils.isBlank(trackingNo)){
                return null;
            }
            // 1.通过快递单号获取是哪个快递公司。
            String typeResult = this.get("http://www.kuaidi100.com/autonumber/autoComNum?text="+trackingNo);
            JSONObject  typeJsonObject = (JSONObject)JSON.parse(typeResult);
            JSONArray typeDataArray = (JSONArray) typeJsonObject.get("auto");
            if(typeDataArray.isEmpty()){
                return null;
            }
            JSONObject typeObject = (JSONObject)typeDataArray.get(0);
            String type = typeObject.getString("comCode");
            // 2.通过快递公司及快递单号获取物流信息。
            String kuaidiResult = this.get("http://www.kuaidi100.com/query?type="+type+"&postid="+trackingNo);
            JSONObject jsonObject = (JSONObject)JSON.parse(kuaidiResult);
            String status = jsonObject.getString("status");
            if(!"200".equals(status)){
                return null;
            }
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            JSONArray dataArray = (JSONArray) jsonObject.get("data");
            for (int i = 0; i < dataArray.size(); i++) {
                Map<String, String> tempMap = new HashMap<String, String>();
                JSONObject temp = (JSONObject) dataArray.get(i);
                tempMap.put("date", (String) temp.get("time"));
                tempMap.put("logisticsInfo", (String) temp.get("context"));
                list.add(tempMap);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

 

 

//以上代码已失效,最新破解版快递100     jdk1.8

package com.dongfang.common.utils.wuliu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.kagura.JJsoup;
import me.kagura.Session;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;

import com.alibaba.fastjson.JSONPath;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONArray;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;


public class Kuaidi100 {
    
    //创建新的Session对象,等价于requests.Session()
    public static Session session = JJsoup.newSession();
    
    public static void main(String[] angs){
        List<Map<String, Object>> dszf = queryLogisticsInfoByKuadi100("71580389924820");
        System.out.println(dszf.toString());
    }
    
    private static String get(String url) throws ClientProtocolException, IOException{
        // 动态模拟请求数据
        /*CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 模拟浏览器浏览(user-agent的值可以通过浏览器浏览,查看发出请求的头文件获取)
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
        CloseableHttpResponse response = httpclient.execute(httpGet);*/
        
        Connection.Response response = session.connect(url)
                .method(Connection.Method.GET)
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                .header("Upgrade-Insecure-Requests", "1")
                .header("Connection", "keep-alive")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
                .header("Referer", "http://www.kuaidi100.com/?from=openv")
                .header("Host", "www.kuaidi100.com")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .execute();
        
        // 获取响应状态码
        int statusCode = response.statusCode();
        ///HttpEntity entity = response.getEntity();
        String html = "";
        // 如果状态响应码为200,则获取html实体内容或者json文件
        if(statusCode == 200){
            html = response.body();
        }
        
        return html;
    }
    
    public void getSession(){
         System.setProperty("java.awt.headless", "false");
         session.ignoreContentType(true);
    }
    
    
    public static List<Map<String, Object>> queryLogisticsInfoByKuadi100(String trackingNo) {
        try {
            
             System.setProperty("java.awt.headless", "false");
             session.ignoreContentType(true);
            if(StringUtils.isBlank(trackingNo)){
                return null;
            }
            
         // 1.通过快递单号获取是哪个快递公司。
            String sdfhg = Kuaidi100.get("http://www.kuaidi100.com/?from=openv");
            
            
            // 1.通过快递单号获取是哪个快递公司。
            String typeResult = Kuaidi100.get("http://www.kuaidi100.com/autonumber/autoComNum?text="+trackingNo);
            JSONObject  typeJsonObject = (JSONObject)JSON.parse(typeResult);
            JSONArray typeDataArray = (JSONArray) typeJsonObject.get("auto");
            if(typeDataArray.isEmpty()){
                return null;
            }
            JSONObject typeObject = (JSONObject)typeDataArray.get(0);
            String type = typeObject.getString("comCode");
            // 2.通过快递公司及快递单号获取物流信息。
            String kuaidiResult = Kuaidi100.get("http://www.kuaidi100.com/query?type="+type+"&postid="+trackingNo+"&temp=0."+System.currentTimeMillis()+"&phone=");
            JSONObject jsonObject = (JSONObject)JSON.parse(kuaidiResult);
            String status = jsonObject.getString("status");
            String nu = jsonObject.getString("nu");
            
            if(!"200".equals(status)){
                return null;
            }
            
            //查询物流单号如果返回的物流单号不是查询的单号重新查询
            while(!trackingNo.equals(nu)){
                // 2.通过快递公司及快递单号获取物流信息。
                kuaidiResult = Kuaidi100.get("http://www.kuaidi100.com/query?type="+type+"&postid="+trackingNo);
                jsonObject = (JSONObject)JSON.parse(kuaidiResult);
                status = jsonObject.getString("status");
                nu = jsonObject.getString("nu");
                if(!"200".equals(status)){
                    return null;
                }
            }
            
            
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            JSONArray dataArray = (JSONArray) jsonObject.get("data");
            for (int i = 0; i < dataArray.size(); i++) {
                Map<String, Object> tempMap = new HashMap<String, Object>();
                JSONObject temp = (JSONObject) dataArray.get(i);
                tempMap.put("date", (String) temp.get("time"));
                tempMap.put("logisticsInfo", (String) temp.get("context"));
                list.add(tempMap);
            }
            
            System.err.println("登录后的所有cookie:"+System.lineSeparator()+session.cookies());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
}
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值