用CloseableHttpClient写一个httpUtil请求类
时间: 2024-01-05 16:03:05 浏览: 39
好的,下面是一个用CloseableHttpClient实现的HttpUtil请求类的示例代码:
```java
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpUtil {
/**
* 发送GET请求
*
* @param url 请求地址
* @return 响应结果
* @throws IOException
*/
public static String doGet(String url) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
}
} finally {
if (response != null) {
response.close();
}
httpClient.close();
}
return null;
}
/**
* 发送POST请求
*
* @param url 请求地址
* @param data 请求参数,JSON格式
* @return 响应结果
* @throws IOException
*/
public static String doPost(String url, String data) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(data, "utf-8");
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json");
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
return EntityUtils.toString(responseEntity);
}
} finally {
if (response != null) {
response.close();
}
httpClient.close();
}
return null;
}
}
```
使用HttpUtil类的示例代码如下:
```java
public class Test {
public static void main(String[] args) throws IOException {
// 发送GET请求
String response1 = HttpUtil.doGet("http://www.baidu.com");
System.out.println(response1);
// 发送POST请求
String data = "{\"name\":\"张三\",\"age\":20}";
String response2 = HttpUtil.doPost("http://www.example.com/api/user", data);
System.out.println(response2);
}
}
```
这段代码中,我们定义了两个方法doGet和doPost,分别用于发送GET请求和POST请求。在发送请求之前,我们创建了一个CloseableHttpClient实例,然后创建HttpGet或HttpPost实例,设置请求头和请求参数,最后执行请求并获取响应结果。在使用完毕后,我们需要关闭CloseableHttpResponse和CloseableHttpClient实例,以释放资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)