# HttpClient使用詳解與實戰一:普通的GET和POST請求
# 簡介
HttpClient是Apache Jakarta Common下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,并且它支持HTTP協議最新的版本和建議。
HttpClient最新版本是HttpClient 4.5.3 (GA)。
官方下載:[http://hc.apache.org/downloads.cgi](https://link.jianshu.com/?t=http://hc.apache.org/downloads.cgi)
# 主要特性
* 基于標準、純凈的Java語言,實現了HTTP1.0和HTTP1.1。
* 以可擴展的面向對象的結構實現了HTTP全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
* 支持加密的HTTPS協議(HTTP通過SSL協議)。
* 通過HTTP代理方式建立透明的連接。
* 利用CONNECT方法通過HTTP代理建立隧道的HTTPS連接。
* Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos認證方案。
* 插件式的自定義認證方案。
* 可插拔的安全套接字工廠,使得接入第三方解決方案變得更容易
* 連接管理支持使用多線程的的應用。支持設置最大連接數,同時支持設置每個主機的最大連接數,發現并關閉過期的連接。
* 自動化處理Set-Cookie:來自服務器的頭,并在適當的時候將它們發送回cookie。
* 可以自定義Cookie策略的插件化機制。
* Request的輸出流可以避免流中內容體直接從socket緩沖到服務器。
* Response的輸入流可以有效的從socket服務器直接讀取相應內容。
* 在HTTP1.0和HTTP1.1中使用用KeepAlive來保持持久連接。
* 可以直接獲取服務器發送的響應碼和響應頭部。
* 具備設置連接超時的能力。
* 支持HTTP/1.1 響應緩存。
* 源代碼基于Apache License 可免費獲取。
# 一般使用步驟
使用HttpClient發送請求、接收響應,一般需要以下步驟。
**HttpGet請求響應的一般步驟:**
1). 創建`HttpClient`對象,可以使用`HttpClients.createDefault()`;
2). 如果是無參數的GET請求,則直接使用構造方法`HttpGet(String url)`創建`HttpGet`對象即可;
如果是帶參數GET請求,則可以先使用`URIBuilder(String url)`創建對象,再調用`addParameter(String param, String value)`,或`setParameter(String param, String value)`來設置請求參數,并調用build()方法構建一個URI對象。只有構造方法`HttpGet(URI uri)`來創建HttpGet對象。
3). 創建`HttpResponse`,調用`HttpClient`對象的`execute(HttpUriRequest request)`發送請求,該方法返回一個`HttpResponse`。調用`HttpResponse`的`getAllHeaders()、getHeaders(String name)`等方法可獲取服務器的響應頭;調用`HttpResponse`的`getEntity()`方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。通過調用`getStatusLine().getStatusCode()`可以獲取響應狀態碼。
4). 釋放連接。
**HttpPost請求響應的一般步驟:**
1). 創建`HttpClient`對象,可以使用`HttpClients.createDefault()`;
2). 如果是無參數的GET請求,則直接使用構造方法`HttpPost(String url)`創建`HttpPost`對象即可;
如果是帶參數POST請求,先構建HttpEntity對象并設置請求參數,然后調用setEntity(HttpEntity entity)創建HttpPost對象。
3). 創建`HttpResponse`,調用`HttpClient`對象的`execute(HttpUriRequest request)`發送請求,該方法返回一個`HttpResponse`。調用`HttpResponse`的`getAllHeaders()、getHeaders(String name)`等方法可獲取服務器的響應頭;調用`HttpResponse`的`getEntity()`方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。通過調用`getStatusLine().getStatusCode()`可以獲取響應狀態碼。
4). 釋放連接。
# 實例代碼實戰
構建一個Maven項目,引入如下依賴
~~~xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
~~~
# 實例1:普通的無參數GET請求
打開一個url,抓取響應結果輸出成html文件
~~~csharp
/**
*普通的GET請求
*/
public class DoGET {
public static void main(String[] args) throws Exception {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建http GET請求
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
//請求體內容
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//內容寫入文件
FileUtils.writeStringToFile(new File("E:\\devtest\\baidu.html"), content, "UTF-8");
System.out.println("內容長度:"+content.length());
}
} finally {
if (response != null) {
response.close();
}
//相當于關閉瀏覽器
httpclient.close();
}
}
}
~~~
# 實例2:執行帶參數的GET請求
模擬使用百度搜索關鍵字"java",并保存搜索結果為html文件
~~~swift
import java.io.File;
import java.net.URI;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 帶參數的GET請求
* 兩種方式:
* 1.直接將參數拼接到url后面 如:?wd=java
* 2.使用URI的方法設置參數 setParameter("wd", "java")
*/
public class DoGETParam {
public static void main(String[] args) throws Exception {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 定義請求的參數
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
// 創建http GET請求
HttpGet httpGet = new HttpGet(uri);
//response 對象
CloseableHttpResponse response = null;
try {
// 執行http get請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//內容寫入文件
FileUtils.writeStringToFile(new File("E:\\devtest\\baidu-param.html"), content, "UTF-8");
System.out.println("內容長度:"+content.length());
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
~~~
# 實例3:執行普通的POST請求
無參數的POST請求,并設置Header來偽裝瀏覽器請求
~~~swift
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
/**
* 常規post請求
* 可以設置Header來偽裝瀏覽器請求
*/
public class DoPOST {
public static void main(String[] args) throws Exception {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
//偽裝瀏覽器請求
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpPost);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//內容寫入文件
FileUtils.writeStringToFile(new File("E:\\devtest\\oschina.html"), content, "UTF-8");
System.out.println("內容長度:"+content.length());
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
~~~
# 實例4:執行帶參數的POST請求
模擬開源中國檢索java,并偽裝瀏覽器請求,輸出響應結果為html文件

檢索java
~~~swift
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* 帶有參數的Post請求
* NameValuePair
*/
public class DoPOSTParam {
public static void main(String[] args) throws Exception {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
// 設置2個post參數,一個是scope、一個是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
// 構造一個form表單式的實體
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 將請求實體設置到httpPost對象中
httpPost.setEntity(formEntity);
//偽裝瀏覽器
httpPost.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpPost);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//內容寫入文件
FileUtils.writeStringToFile(new File("E:\\devtest\\oschina-param.html"), content, "UTF-8");
System.out.println("內容長度:"+content.length());
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
~~~
# 總結
本文介紹了HttpClient的特性,是按照官方英文文檔翻譯而來,然后分別介紹了HttpGet和HttpPost的一般使用步驟,最后給出了4個簡單的實例的Java代碼。下一章節我們會介紹HttpClient連接池管理以及Spring整合HttpClient的具體過程。