HttpClient工具類
阿新 • • 發佈:2020-06-24
HttpClient工具類
1. 什麼是HttpClient
HTTP 協議可能是現在 Internet 上使用得最多、最重要的協議了,越來越多的 Java 應用程式需要直接通過 HTTP 協議來訪問網路資源。雖然在 JDK 的 java net包中已經提供了訪問 HTTP 協議的基本功能,但是對於大部分應用程式來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 是Apache HttpComponents 下的子專案,用來提供高效的、最新的、功能豐富的支援 HTTP 協議的客戶端程式設計工具包,並且它支援 HTTP 協議最新的版本和建議。
2. 功能介紹
- 實現了所有 HTTP 的方法(GET,POST,PUT,DELETE 等)
- 支援自動轉向
- 支援 HTTPS 協議
- 支援代理伺服器等
3. 版本比較
主要是基於 HttpClient4.5.5 版本的來講解的,也是現在最新的版本,之所以要提供版本說明的是因為 HttpClient 3 版本和 HttpClient 4 版本差別還是很多大的,基本HttpClient裡面的介面都變了,你把 HttpClient 3 版本的程式碼拿到 HttpClient 4 上面都執行不起來,會報錯的。所以一定要注意 HtppClient 的版本問題。
4. HttpClient不能做的事情
HttpClient 不是瀏覽器,它是一個客戶端 HTTP 協議傳輸類庫。HttpClient 被用來傳送和接受 HTTP 訊息。HttpClient 不會處理 HTTP 訊息的內容,不會進行 javascript 解析,不會關心 content type,如果沒有明確設定,HttpClient 也不會對請求進行格式化、重定向 url,或者其他任何和 HTTP 訊息傳輸相關的功能。
5. HttpClient使用流程
使用HttpClient傳送請求、接收響應很簡單,一般需要如下幾步即可。
- 建立HttpClient物件。
- 建立請求方法的例項,並指定請求URL。如果需要傳送GET請求,建立HttpGet物件;如果需要傳送POST請求,建立HttpPost物件。
- 如果需要傳送請求引數,可呼叫HttpGetsetParams方法來新增請求引數;對於HttpPost物件而言,可呼叫setEntity(HttpEntity entity)方法來設定請求引數。
- 呼叫HttpClient物件的execute(HttpUriRequest request)傳送請求,該方法返回一個HttpResponse物件。
- 呼叫HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取伺服器的響應頭;呼叫HttpResponse的getEntity()方法可獲取HttpEntity物件,該物件包裝了伺服器的響應內容。程式可通過該物件獲取伺服器的響應內容。
- 釋放連線。無論執行方法是否成功,都必須釋放連線
<properties>
<httpclient.version>4.5.5</httpclient.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- springboot的web和test啟動庫 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- apache httpclient元件 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 跳過單元測試 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
複製程式碼
6.2 編寫spring-boot啟動類
/**
* Description: springboot啟動類
*
* @author JourWon
* @date Created on 2018年4月19日
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
複製程式碼
6.3 編寫get和post請求測試controller
/**
* Description: get和post請求測試controller
*
* @author JourWon
* @date Created on 2018年4月19日
*/
@RestController
@RequestMapping("/hello")
public class HelloWorldController {
@GetMapping("/get")
public String get() throws InterruptedException {
return "get無參請求成功";
}
@GetMapping("/getWithParam")
public String getWithParam(@RequestParam String message) {
return "get帶參請求成功,引數message: " + message;
}
@PostMapping("/post")
public String post(@RequestHeader("User-Agent") String userAgent,@RequestHeader("Accept") String accept,@RequestHeader("Accept-Language") String acceptLanguage,@RequestHeader("Accept-Encoding") String acceptEncoding,@RequestHeader("Cookie") String cookie,@RequestHeader("Connection") String conn) {
// 列印請求頭資訊
System.out.println("Cookie = " + cookie);
System.out.println("Connection = " + conn);
System.out.println("Accept = " + accept);
System.out.println("Accept-Language = " + acceptLanguage);
System.out.println("Accept-Encoding = " + acceptEncoding);
System.out.println("User-Agent = " + userAgent);
return "post無參請求成功";
}
@PostMapping("/postWithParam")
public String postWithParam(@RequestParam String code,@RequestParam String message) {
return "post帶參請求成功,引數code: " + code + ",引數message: " + message;
}
}
複製程式碼
6.4 建立httpClient響應結果物件
/**
* Description: 封裝httpClient響應結果
*
* @author JourWon
* @date Created on 2018年4月19日
*/
public class HttpClientResult implements Serializable {
/**
* 響應狀態碼
*/
private int code;
/**
* 響應資料
*/
private String content;
}
複製程式碼
6.5 重點,編寫httpclient工具類
/**
* Description: httpClient工具類
*
* @author JourWon
* @date Created on 2018年4月19日
*/
public class HttpClientUtils {
// 編碼格式。傳送編碼格式統一用UTF-8
private static final String ENCODING = "UTF-8";
// 設定連線超時時間,單位毫秒。
private static final int CONNECT_TIMEOUT = 6000;
// 請求獲取資料的超時時間(即響應時間),單位毫秒。
private static final int SOCKET_TIMEOUT = 6000;
/**
* 傳送get請求;不帶請求頭和請求引數
*
* @param url 請求地址
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url) throws Exception {
return doGet(url,null,null);
}
/**
* 傳送get請求;帶請求引數
*
* @param url 請求地址
* @param params 請求引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url,Map<String,String> params) throws Exception {
return doGet(url,params);
}
/**
* 傳送get請求;帶請求頭和請求引數
*
* @param url 請求地址
* @param headers 請求頭集合
* @param params 請求引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url,String> headers,String> params) throws Exception {
// 建立httpClient物件
CloseableHttpClient httpClient = HttpClients.createDefault();
// 建立訪問的地址
URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) {
Set<Entry<String,String>> entrySet = params.entrySet();
for (Entry<String,String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(),entry.getValue());
}
}
// 建立http物件
HttpGet httpGet = new HttpGet(uriBuilder.build());
/**
* setConnectTimeout:設定連線超時時間,單位毫秒。
* setConnectionRequestTimeout:設定從connect Manager(連線池)獲取Connection
* 超時時間,單位毫秒。這個屬性是新加的屬性,因為目前版本是可以共享連線池的。
* setSocketTimeout:請求獲取資料的超時時間(即響應時間),單位毫秒。 如果訪問一個介面,多少時間內無法返回資料,就直接放棄此次呼叫。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpGet.setConfig(requestConfig);
// 設定請求頭
packageHeader(headers,httpGet);
// 建立httpResponse物件
CloseableHttpResponse httpResponse = null;
try {
// 執行請求並獲得響應結果
return getHttpClientResult(httpResponse,httpClient,httpGet);
} finally {
// 釋放資源
release(httpResponse,httpClient);
}
}
/**
* 傳送post請求;不帶請求頭和請求引數
*
* @param url 請求地址
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url) throws Exception {
return doPost(url,null);
}
/**
* 傳送post請求;帶請求引數
*
* @param url 請求地址
* @param params 引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url,String> params) throws Exception {
return doPost(url,params);
}
/**
* 傳送post請求;帶請求頭和請求引數
*
* @param url 請求地址
* @param headers 請求頭集合
* @param params 請求引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url,String> params) throws Exception {
// 建立httpClient物件
CloseableHttpClient httpClient = HttpClients.createDefault();
// 建立http物件
HttpPost httpPost = new HttpPost(url);
/**
* setConnectTimeout:設定連線超時時間,單位毫秒。
* setConnectionRequestTimeout:設定從connect Manager(連線池)獲取Connection
* 超時時間,單位毫秒。這個屬性是新加的屬性,因為目前版本是可以共享連線池的。
* setSocketTimeout:請求獲取資料的超時時間(即響應時間),單位毫秒。 如果訪問一個介面,多少時間內無法返回資料,就直接放棄此次呼叫。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig);
// 設定請求頭
/*httpPost.setHeader("Cookie","");
httpPost.setHeader("Connection","keep-alive");
httpPost.setHeader("Accept","application/json");
httpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9");
httpPost.setHeader("Accept-Encoding","gzip,deflate,br");
httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/65.0.3325.181 Safari/537.36");*/
packageHeader(headers,httpPost);
// 封裝請求引數
packageParam(params,httpPost);
// 建立httpResponse物件
CloseableHttpResponse httpResponse = null;
try {
// 執行請求並獲得響應結果
return getHttpClientResult(httpResponse,httpPost);
} finally {
// 釋放資源
release(httpResponse,httpClient);
}
}
/**
* 傳送put請求;不帶請求引數
*
* @param url 請求地址
* @param params 引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doPut(String url) throws Exception {
return doPut(url);
}
/**
* 傳送put請求;帶請求引數
*
* @param url 請求地址
* @param params 引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doPut(String url,String> params) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPut.setConfig(requestConfig);
packageParam(params,httpPut);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse,httpPut);
} finally {
release(httpResponse,httpClient);
}
}
/**
* 傳送delete請求;不帶請求引數
*
* @param url 請求地址
* @param params 引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doDelete(String url) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpDelete.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse,httpDelete);
} finally {
release(httpResponse,httpClient);
}
}
/**
* 傳送delete請求;帶請求引數
*
* @param url 請求地址
* @param params 引數集合
* @return
* @throws Exception
*/
public static HttpClientResult doDelete(String url,String> params) throws Exception {
if (params == null) {
params = new HashMap<String,String>();
}
params.put("_method","delete");
return doPost(url,params);
}
/**
* Description: 封裝請求頭
* @param params
* @param httpMethod
*/
public static void packageHeader(Map<String,String> params,HttpRequestBase httpMethod) {
// 封裝請求頭
if (params != null) {
Set<Entry<String,String> entry : entrySet) {
// 設定到請求頭到HttpRequestBase物件中
httpMethod.setHeader(entry.getKey(),entry.getValue());
}
}
}
/**
* Description: 封裝請求引數
*
* @param params
* @param httpMethod
* @throws UnsupportedEncodingException
*/
public static void packageParam(Map<String,HttpEntityEnclosingRequestBase httpMethod)
throws UnsupportedEncodingException {
// 封裝請求引數
if (params != null) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<Entry<String,String> entry : entrySet) {
nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
// 設定到請求的http物件中
httpMethod.setEntity(new UrlEncodedFormEntity(nvps,ENCODING));
}
}
/**
* Description: 獲得響應結果
*
* @param httpResponse
* @param httpClient
* @param httpMethod
* @return
* @throws Exception
*/
public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,CloseableHttpClient httpClient,HttpRequestBase httpMethod) throws Exception {
// 執行請求
httpResponse = httpClient.execute(httpMethod);
// 獲取返回結果
if (httpResponse != null && httpResponse.getStatusLine() != null) {
String content = "";
if (httpResponse.getEntity() != null) {
content = EntityUtils.toString(httpResponse.getEntity(),ENCODING);
}
return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(),content);
}
return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
/**
* Description: 釋放資源
*
* @param httpResponse
* @param httpClient
* @throws IOException
*/
public static void release(CloseableHttpResponse httpResponse,CloseableHttpClient httpClient) throws IOException {
// 釋放資源
if (httpResponse != null) {
httpResponse.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
複製程式碼
6.6 啟動spring-boot,測試get、post請求
/**
* Description: HttpClientUtils工具類測試
*
* @author JourWon
* @date Created on 2018年4月19日
*/
public class HttpClientUtilsTest {
/**
* Description: 測試get無參請求
*
* @throws Exception
*/
@Test
public void testGet() throws Exception {
HttpClientResult result = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/get");
System.out.println(result);
}
/**
* Description: 測試get帶參請求
*
* @throws Exception
*/
@Test
public void testGetWithParam() throws Exception {
Map<String,String> params = new HashMap<String,String>();
params.put("message","helloworld");
HttpClientResult result = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/getWithParam",params);
System.out.println(result);
}
/**
* Description: 測試post帶請求頭不帶請求引數
*
* @throws Exception
*/
@Test
public void testPost() throws Exception {
Map<String,String> headers = new HashMap<String,String>();
headers.put("Cookie","123");
headers.put("Connection","keep-alive");
headers.put("Accept","application/json");
headers.put("Accept-Language",zh;q=0.9");
headers.put("User-Agent",like Gecko) Chrome/65.0.3325.181 Safari/537.36");
HttpClientResult result = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/post",headers,null);
System.out.println(result);
}
/**
* Description: 測試post帶參請求
*
* @throws Exception
*/
@Test
public void testPostWithParam() throws Exception {
Map<String,String>();
params.put("code","0");
params.put("message","helloworld");
HttpClientResult result = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/postWithParam",params);
System.out.println(result);
}
}
複製程式碼
HttpsUtils工具類
實現利用HttpClient傳送Https請求,信任任何證書、不對主機校驗
參見官方檔案
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
private static SSLConnectionSocketFactory sslsf = null;
private static PoolingHttpClientConnectionManager cm = null;
private static SSLContextBuilder builder = null;
static {
try {
builder = new SSLContextBuilder();
// 全部信任 不做身份鑑定
builder.loadTrustMaterial(null,(org.apache.http.ssl.TrustStrategy) (x509Certificates,s) -> true);
sslsf = new SSLConnectionSocketFactory(builder.build(),new String[]{"SSLv2Hello","SSLv3","TLSv1","TLSv1.2"},null,NoopHostnameVerifier.INSTANCE);
//設定協議http和https對應的處理socket連線工廠的物件
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTP,new PlainConnectionSocketFactory())
.register(HTTPS,sslsf)
.build();
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(200);//max connection
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* httpClient get請求
* @param url 請求url
* @param
* @param
* @return 可能為空 需要處理
* @throws Exception
*
*/
public static String get(String url) throws Exception {
String result = "";
CloseableHttpClient httpClient = null;
try {
httpClient = getHttpClient();
HttpGet httpGet = new HttpGet(url);
// 設定頭資訊,必須新增,否則會報403
httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/77.0.3865.90 Safari/537.36");
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity,"utf-8");
} else {
readHttpResponse(httpResponse);
}
} catch (Exception e) {throw e;
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return result;
}
public static CloseableHttpClient getHttpClient() throws Exception {
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setConnectionManager(cm)
.setConnectionManagerShared(true)
.build();
return httpClient;
}
public static String readHttpResponse(HttpResponse httpResponse)
throws ParseException,IOException {
StringBuilder builder = new StringBuilder();
// 獲取響應訊息實體
HttpEntity entity = httpResponse.getEntity();
// 響應狀態
builder.append("status:" + httpResponse.getStatusLine());
builder.append("headers:");
HeaderIterator iterator = httpResponse.headerIterator();
while (iterator.hasNext()) {
builder.append("\t" + iterator.next());
}
// 判斷響應實體是否為空
if (entity != null) {
String responseString = EntityUtils.toString(entity);
builder.append("response length:" + responseString.length());
builder.append("response content:" + responseString.replace("\r\n",""));
}
return builder.toString();
}
}
複製程式碼
參考github開源工具