HttpClient 的实践

一、HttpClient 简介

HttpClientApache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

二、Maven依赖

<dependency>
   <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.10</version>
</dependency>

三、GET请求

private static void get() {
// 创建 HttpClient 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建 HttpGet 请求
HttpGet httpGet = new HttpGet("https//xxxxxxxx.com");
// 设置长连接
httpGet.setHeader("Connection", "keep-alive");
// 设置代理(模拟浏览器版本)
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
// 设置 Cookie
//httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

CloseableHttpResponse httpResponse = null;
try {
    // 请求并获得响应结果
    httpResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = httpResponse.getEntity();
    // 输出请求结果
    System.out.println(EntityUtils.toString(httpEntity));
} catch (IOException e) {
    e.printStackTrace();
}

// 无论如何必须关闭连接
finally {
    if (httpResponse != null) {
        try {
            httpResponse.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

四、POST请求

private static void post() {
    // 创建 HttpClient 客户端
    CloseableHttpClient httpClient = HttpClients.createDefault();

    // 创建 HttpPost 请求
    HttpPost httpPost = new HttpPost("http://www.xxxxxxx.com");
    // 设置长连接
    httpPost.setHeader("Connection", "keep-alive");
    // 设置代理(模拟浏览器版本)
    httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
    // 设置 Cookie
    //httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

    // 创建 HttpPost 参数
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("draw", "1"));
    params.add(new BasicNameValuePair("start", "0"));
    params.add(new BasicNameValuePair("length", "10"));

    CloseableHttpResponse httpResponse = null;
    try {
        // 设置 HttpPost 参数
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        // 输出请求结果
        System.out.println(EntityUtils.toString(httpEntity));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // 无论如何必须关闭连接
    finally {
        try {
            if (httpResponse != null) {
                httpResponse.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

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

你可能感兴趣的:(Java)