韵达快递

import net.sf.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

public class Yunda {

    public static void main(String[] args) {
        String code = createCode();
        String[] mailnos = {""};

        System.out.println(code);

        for(String mailno : mailnos){
            JSONObject data = query(code, mailno);
            System.out.println(data);
        }

    }

    /**
     * 查询物流信息
     * @param code
     * @param mailno
     * @return
     */
    private static JSONObject query(String code, String mailno){
        String url = "http://op.yundasys.com/ydorder/queryWayDetailInfo";


        HttpURLConnection conn = null;
        OutputStream out = null;
        StringBuffer sb = new StringBuffer();

        JSONObject body = new JSONObject();
        body.put("accountId", code);
        body.put("accountSrc", "wxapp");
        body.put("createTm", "");
        body.put("imageCode", "");
        body.put("mailno", mailno);
        body.put("reqTime", System.currentTimeMillis());

        try {
            URL aUrl = new URL(url);

            //conn = (HttpURLConnection) aUrl.openConnection(proxy);
            conn = (HttpURLConnection) aUrl.openConnection();

            conn.setUseCaches(false);
            conn.setDefaultUseCaches(false);
            conn.setRequestMethod("POST");

            conn.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            conn.setRequestProperty("content-type", "application/json;charset=UTF-8");
            conn.setRequestProperty("Host", "op.yundasys.com");
            conn.setRequestProperty("Origin", "http://op.yundasys.com");
            conn.setRequestProperty("Referer", "http://op.yundasys.com/opserver/pages/waydetail/waydetail.html?openid="+ code +"&mailno=" + mailno);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");


            conn.setDoOutput(true);
            byte[] data = body.toString().getBytes("UTF-8");
            out = conn.getOutputStream();
            out.write(data);
            out.flush();


            int status = conn.getResponseCode();
            InputStream in = null;
            if (status / 100 == 2) {
                in = conn.getInputStream();
            }
            else {
                in = conn.getErrorStream();
            }

            BufferedReader bufferedReader = null;
            InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            char[] buff = new char[1024];
            int len;
            while ((len = bufferedReader.read(buff)) > 0) {
                sb.append(buff, 0, len);
            }
            bufferedReader.close();

            String responseContent = sb.toString();
            return JSONObject.fromObject(responseContent);
        }
        catch (IOException e) {

            throw new RuntimeException(e.getMessage());

        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    System.out.println("Failed to close stream." +  e);
                }
            }
            if (null != conn) {
                conn.disconnect();
            }
        }
    }

    /**
     * 获取24位随机码
     * @return
     */
    private static String createCode(){
        String code="";
        int codeLength=24;
        Random random = new Random();
        char[] chars=new char[]{'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        int len = chars.length;
        for(int i=0;i<codeLength;i++){
            int charIndex= random.nextInt(len);
            code += chars[charIndex];
        }
        if(code.length() != codeLength){
            return createCode();
        }else{
            return code;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/rubekid/p/13384691.html
今日推荐