Java: HttpURLConnection 使用多线程加快批量Post/Get的效率_20160918_七侠镇莫尛貝

使用多线程,可大大提高批量Post/Get操作的效率。


Post线程类:

class Thread_Http_Post extends Thread{
    public static String cookieVal = ""

    private String httpurl;
    private String cookies;
    private String str_param_body;
    private String charset;
    private boolean b_flag;

    public Thread_Http_Post(String httpurl,String str_param_body,String charset,String cookies,boolean b_flag){
        this.httpurl = httpurl;
        this.cookies = cookies;
        this.b_flag = b_flag;
        this.str_param_body = str_param_body;
        this.charset = charset;
    }

    public void http_Post(){
        HttpURLConnection connection = null;
        InputStream urlStream = null;
        URL url = null;
        DataOutputStream out = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        String lines = null;

        try {
            url = new URL(httpurl);

            connection = (HttpURLConnection)url.openConnection();
            if ("" != cookies) {
                connection.setRequestProperty("Cookie", cookies);
                //    println("set cookies = [" + cookies + "]")
            }
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(b_flag);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            connection.connect();

            out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(str_param_body);
            out.flush();

            cookieVal = connection.getHeaderField("Set-Cookie")
            out.close(); // flush and close

            Thread.sleep(10L);
            // 取得输入流,并使用Reader读取
            if(false){
                urlStream = connection.getInputStream();
                inputStreamReader = new InputStreamReader(  urlStream, "utf-8")
                reader = new BufferedReader(inputStreamReader);
                while ((lines = reader.readLine()) != null) {
                    println(lines);
                }
                println(" ")
                reader.close();
                inputStreamReader.close()
                urlStream.close()
            }
        } catch (InterruptedException e) {
            //
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        http_Post()
    }

}

Get 线程类:

class Thread_Http_Get extends Thread{
    public static String cookieVal = ""

    private String httpurl;
    private String cookies;
    private String str_param_url;
    private String charset;
    private boolean b_flag;

    public Thread_Http_Get(String httpurl,String str_param_url,String charset,String cookies,boolean b_flag){
        this.httpurl = httpurl;
        this.cookies = cookies;
        this.b_flag = b_flag;
        this.str_param_url = str_param_url;
        this.charset = charset;
    }

    public void http_Get(){
        HttpURLConnection connection = null;
        InputStream urlStream = null;
        URL url = null;
        DataOutputStream out = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        String lines = null;

        try {
            httpurl = httpurl + "?" + str_param_url

            url = new URL(httpurl);

            connection = (HttpURLConnection)url.openConnection();
            if ("" != cookies) {
                connection.setRequestProperty("Cookie", cookies);
                //    println("set cookies = [" + cookies + "]")
            }

            connection.connect();

            Thread.sleep(10L);
            // 取得输入流,并使用Reader读取
            urlStream = connection.getInputStream();

            if(true){
                inputStreamReader = new InputStreamReader(  urlStream, "utf-8")
                reader = new BufferedReader(inputStreamReader);
                while ((lines = reader.readLine()) != null) {
                    println(lines);
                }
                println(" ")
                reader.close();
                inputStreamReader.close()
                urlStream.close()
            }
        } catch (InterruptedException e) {
            //
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        http_Get()
    }

}

上传文件线程类:

 

class Thread_Http_PostFile extends Thread{
    public static String cookieVal = ""

    private String httpurl_post;
    private String httpurl_get;
    private String cookies;
    private String str_param_body;
    private String charset;
    private boolean b_flag;

    public static String BOUNDARY = "---------------------------27104325129557"; // 定义数据分隔线

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值