HttpClientBuilder和closeableHttpClient
时间: 2024-06-16 22:02:43 浏览: 22
HttpClientBuilder是HttpClient库中的一个类,用于创建和配置HttpClient对象。它提供了一些方法来设置连接池、超时时间、重试机制等参数,以及添加拦截器等功能。使用HttpClientBuilder可以更灵活地创建和配置HttpClient对象。
CloseableHttpClient是HttpClient库中的一个接口,它继承自HttpClient接口,并且实现了Closeable接口。CloseableHttpClient是HttpClient的一个实现类,它可以发送HTTP请求并接收响应。与HttpClient相比,CloseableHttpClient在发送请求后会自动释放连接,无需手动调用释放连接的方法。
下面是使用HttpClientBuilder创建CloseableHttpClient对象的示例代码[^1]:
```java
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
// 创建HttpClientBuilder对象
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 配置连接池大小
httpClientBuilder.setMaxConnTotal(100);
httpClientBuilder.setMaxConnPerRoute(10);
// 配置超时时间
httpClientBuilder.setConnectTimeout(5000);
httpClientBuilder.setSocketTimeout(5000);
// 添加拦截器
httpClientBuilder.addInterceptorFirst(new MyInterceptor());
// 创建CloseableHttpClient对象
CloseableHttpClient httpClient = httpClientBuilder.build();
```
使用CloseableHttpClient发送GET请求的示例代码:
```java
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;
// 创建CloseableHttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpGet对象,并设置请求URL
HttpGet httpGet = new HttpGet("http://www.example.com");
// 发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpGet);
// 处理响应
// ...
// 释放连接
response.close();
httpClient.close();
```
使用CloseableHttpClient发送POST请求的示例代码:
```java
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
// 创建CloseableHttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpPost对象,并设置请求URL
HttpPost httpPost = new HttpPost("http://www.example.com");
// 设置请求参数
StringEntity entity = new StringEntity("param1=value1¶m2=value2");
httpPost.setEntity(entity);
// 发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpPost);
// 处理响应
// ...
// 释放连接
response.close();
httpClient.close();
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)