HttpClient 详解

2016-09-01· 4629 次浏览
## 基础环境 ### Maven pom ```html <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.3</version> </dependency> ``` ## HttpClient 设置Post参数 ``` HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("aaa", "x7AeuPz6dPPFaaKlXxvWVkiZu2Do25oHmRiLeWFM")); nvps.add(new BasicNameValuePair("bbb", "bbb")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); ``` ## HttpClient 设置Cookie ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Cookie","jsessionid=6B6FE81CCBEB80A0017B11D8ED1915A2"); ``` ## HttpClient 设置Refere ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Referer", "http://www.baidu.com"); ``` ## HttpClient 设置user-agent ``` String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"; CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(userAgent).build(); ``` ## HttpClient 设置超时时间 ### 全局设置 ``` RequestConfig requestConfig = RequestConfig.custom()         .setSocketTimeout(1000)         .setConnectTimeout(1000)         .build(); CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); ``` ### 为单独的HttpGet/Post设置 ``` RequestConfig requestConfig = RequestConfig.custom()         .setSocketTimeout(1000)         .setConnectTimeout(1000)         .build(); HttpGet httpget = new HttpGet("http://localhost/1"); httpget.setConfig(requestConfig); ``` ## HttpClient设置代理 ``` // 代理地址,端口号,协议类型 HttpHost host = new HttpHost(ip, port, "http"); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(host); CloseableHttpClient httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build(); ``` ## 完整示例 ```java package net.xsoftlab.baike; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.impl.conn.DefaultProxyRoutePlanner; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpClientTest {     private final int socketTimeout = 120 * 1000;     private final int connectTimeout = 120 * 1000;     private final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";     /**      * 获取指定Url内容      *       * @param url      * @return      */     private String getResponseBody(String url) throws IOException {         // 创建全局 RequestConfig ,并设置连接超时时间与数据响应超时时间         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout)                 .setSocketTimeout(socketTimeout).build();         // 创建HttpClient         CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(userAgent)                 .setDefaultRequestConfig(requestConfig).build();         // 设置请求参数         List<NameValuePair> nvps = new ArrayList<NameValuePair>();         nvps.add(new BasicNameValuePair("aaa", "x7AeuPz6dPPFaaKlXxvWVkiZu2Do25oHmRiLeWFM"));         nvps.add(new BasicNameValuePair("bbb", "bbb"));         String param = EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8));         // 创建get请求         HttpGet httpGet = new HttpGet(url + "?" + param);         // 设置Cookie         httpGet.setHeader("Cookie", "jsessionid=6B6FE81CCBEB80A0017B11D8ED1915A2");         // 设置Referer         httpGet.setHeader("Referer", "http://www.baidu.com/");         // 获取响应结果         CloseableHttpResponse response = httpClient.execute(httpGet);         // 状态码         int status = response.getStatusLine().getStatusCode();         if (status == HttpStatus.SC_OK) {             return EntityUtils.toString(response.getEntity());         } else {             return "请求失败,status = " + status;         }     }     /**      * 使用代理获取Url内容      *       * @return      * @throws IOException      */     public String getResponseBodyWithProxy(String ip, int port, String url) throws IOException {         // 代理地址,端口号,协议类型         HttpHost host = new HttpHost(ip, port, "http");         DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(host);         CloseableHttpClient httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();         // 创建get请求         HttpGet httpGet = new HttpGet(url);         // 获取响应结果         CloseableHttpResponse response = httpClient.execute(httpGet);         // 状态码         int status = response.getStatusLine().getStatusCode();         if (status == HttpStatus.SC_OK) {             return EntityUtils.toString(response.getEntity());         } else {             return "请求失败,status = " + status;         }     }     public static void main(String[] args) throws IOException {         String url = "http://baike.xsoftlab.net/view/956.html";         HttpClientTest test = new HttpClientTest();         String body = test.getResponseBody(url);         System.out.println("请求地址:" + url);         System.out.println(body);         url = "http://www.google.com/";         System.out.println("\n\n\n");         System.out.println("请求地址:" + url);         body = test.getResponseBodyWithProxy("127.0.0.1", 1080, url);         System.out.println(body);     } } ```