HttpClient

不努力,誰會可憐你?發表於2020-11-06

目錄

封裝HttpClient

Ⅰ、連線池引數說明

Ⅱ、配置資訊說明

Ⅲ、呼叫說明

Ⅲ、封裝程式碼

用Cookie(SessionId)登入

Jsoup解析請求結果


封裝HttpClient

Ⅰ、連線池引數說明

Ⅱ、配置資訊說明

Ⅲ、呼叫說明

1、使用設定引數的Get請求

//需要構建一個設定好引數的uriBuider

2、使用帶參Post請求

if (!openId.isEmpty() && !token.isEmpty()) {
            Map<String, Object> param = new HashMap<>();
            param.put("touser", openId);
            param.put("template_id", "Jnb2e_raye3y9_FBUHmx3PR6izdRuyQIFEhRk");
            param.put("page", "pages/logs/logs");

            String result = HttpUtil.sendPost(url, JSON.toJSONString(param));//需要將物件專為json串
            System.out.println("請求結果:" + result);

}

Ⅲ、封裝程式碼

package com.yun.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;

public class HttpUtil {
    //連線池管理器
    private PoolingHttpClientConnectionManager cm;
    //代理訪問
    private  static HttpHost proxy = new HttpHost("183.154.49.68", 9999);
    //請求配置
    private  static final RequestConfig CONFIG = RequestConfig.custom()
            .setConnectTimeout(5*1000) //建立連結的最長時間
            .setConnectionRequestTimeout(5*1000) //設定獲取連結的最長時間
            .setSocketTimeout(10*1000).build(); //設定資料傳輸的最長時間
//            .setProxy(proxy).build(); //設定代理

    public HttpUtil() {
        this.cm = new PoolingHttpClientConnectionManager();
    }

    /**
     * 傳送不帶引數的HttpGet請求
     *
     * @param url
     * @return
     */
    public String sendGet(String url) {
        //獲得連結
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(this.cm).build();
        HttpGet httpget = new HttpGet(url);
        //加入配置
        httpget.setConfig(CONFIG);
        //設定請求頭
        httpPost.setHeader("Host", "mobile.yangkeduo.com");
//        httpget.setHeader("Connection", "keep-alive");
//        httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
//        httpget.setHeader("Origin", "https://mobile.yangkeduo.com");
//        httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
//        httpget.setHeader("Content-Type", "application/json;charset=UTF-8");
//        httpget.setHeader("Sec-Fetch-Site", "same-origin");
//        httpget.setHeader("Sec-Fetch-Mode", "navigate");
//        httpget.setHeader("Referer", "https://mobile.yangkeduo.com/?refer_page_name=login&refer_page_id=10169_1576556331450_ryLM6CZ3Od&refer_page_sn=10169");
//        httpget.setHeader("Accept-Encoding", "gzip, deflate, br");
//        httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
//        httpget.setHeader("Cookie", "");

        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            HttpEntity entity = response.getEntity();
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                System.out.println("狀態碼為非200!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

    /**
     * 傳送帶引數的HttpGet請求
     *
     * @param url
     * @return
     */
    public String sendGet(URIBuilder uriBuilder) throws URISyntaxException {
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(this.cm).build();
        HttpGet httpget = new HttpGet(uriBuilder.build());
        httpget.setConfig(CONFIG);

        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            HttpEntity entity = response.getEntity();
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                System.out.println("狀態碼為非200!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }


    /**
     * 傳送不帶引數的HttpPost請求
     *
     * @param url
     * @return
     */
    public String sendPost(String url) {
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(this.cm).build();
        HttpPost httppost = new HttpPost(url);
        httppost.setConfig(CONFIG);

        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 傳送帶引數的HttpPost請求,引數為json
     *
     * @param url
     * @param map
     * @return
     */
    public String sendPost(String url, String json) {
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(this.cm).build();

        String returnValue = "這是預設返回值,介面呼叫失敗";
//        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(CONFIG);

            //第三步:給httpPost設定JSON格式的引數
            StringEntity requestEntity = new StringEntity(json, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);

            //第四步:傳送HttpPost請求,獲取返回值
            returnValue = httpClient.execute(httpPost, responseHandler); //調介面獲取返回值時,必須用此方法

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //第五步:處理返回值
        return returnValue;
    }
}

用Cookie(SessionId)登入

package login;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class CookieLogin {
    public static void main(String[] args) throws Exception {

        //1、建立httpClient例項
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2、建立請求
        HttpGet httpPost = new HttpGet("https://mobile.yangkeduo.com/?refer_page_name=login&refer_page_id=10169_1576556331450_ryLM6CZ3Od&refer_page_sn=10169");
        //設定代理訪問
        HttpHost proxy = new HttpHost("183.154.49.68", 9999);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        //設定請求頭
//        httpPost.setHeader("Host", "mobile.yangkeduo.com");
        httpPost.setHeader("Connection", "keep-alive");
        httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        httpPost.setHeader("Origin", "https://mobile.yangkeduo.com");
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Sec-Fetch-Site", "same-origin");
        httpPost.setHeader("Sec-Fetch-Mode", "navigate");
        httpPost.setHeader("Referer", "https://mobile.yangkeduo.com/?refer_page_name=login&refer_page_id=10169_1576556331450_ryLM6CZ3Od&refer_page_sn=10169");
        httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpPost.setHeader("Cookie", "");
        try {
            //3、開始請求
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            String strResult = "";
            if(httpResponse != null){
                System.out.println(httpResponse.getStatusLine().getStatusCode());
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                    strResult = EntityUtils.toString(httpResponse.getEntity());
                } else if (httpResponse.getStatusLine().getStatusCode() == 400) {
                    strResult = "Error Response: " + httpResponse.getStatusLine().toString();
                } else if (httpResponse.getStatusLine().getStatusCode() == 500) {
                    strResult = "Error Response: " + httpResponse.getStatusLine().toString();
                } else {
                    strResult = "Error Response: " + httpResponse.getStatusLine().toString();
                }
            }else{
                System.out.println("httpResponse的資料為空!");
            }
            System.out.println(strResult);

        } catch (Exception e) {
            e.printStackTrace();

        }finally {
            try {
                if(httpClient != null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

Jsoup解析請求結果

依賴包

案例

1、獲取元素【方式一】




2、獲取元素【方式二】





3、獲取元素【方式二】的組合使用




//查詢間接子元素
//查詢直接子元素

4、獲取元素中的屬性、屬性值、資料




 

相關文章