类似CloseableHttpClient 的http客户端请求类
时间: 2023-12-19 11:28:39 浏览: 32
类似CloseableHttpClient的HTTP客户端请求类有很多,其中比较常用的有以下几种:
1. HttpURLConnection:Java自带的HTTP客户端请求类,可以通过URL.openConnection()方法获取连接对象,支持GET、POST等请求方式。
2. OkHttp:一个开源的HTTP客户端请求库,支持同步、异步请求,支持连接池、缓存等功能。
3. Apache HttpClient:一个开源的HTTP客户端请求库,支持同步、异步请求,支持连接池、缓存等功能,与CloseableHttpClient类似。
4. Retrofit:一个基于OkHttp的RESTful风格的HTTP客户端请求库,支持注解方式定义请求接口,支持同步、异步请求,支持连接池、缓存等功能。
以上是常用的HTTP客户端请求类,每个类都有其特点和适用场景,具体使用需要根据实际情况选择。
相关问题
CloseableHttpClient 带cookie请求
您可以使用 CloseableHttpClient 来发送带有 cookie 的请求。下面是一个示例代码,展示了如何使用 CloseableHttpClient 和 BasicCookieStore 来发送带有 cookie 的请求:
```java
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientWithCookieExample {
public static void main(String[] args) {
// 创建一个 CookieStore 对象用于存储 cookie
CookieStore cookieStore = new BasicCookieStore();
// 创建一个 HttpClient 对象
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
try {
// 创建一个 HttpGet 请求
HttpGet httpGet = new HttpGet("https://www.example.com");
// 发送请求并获取响应
CloseableHttpResponse response = httpClient.execute(httpGet);
// 处理响应...
// 关闭响应
response.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
```
在上面的示例中,我们首先创建了一个 CookieStore 对象来存储 cookie。然后,我们使用 HttpClients.custom() 方法创建了一个自定义的 CloseableHttpClient 对象,并通过 setDefaultCookieStore() 方法将 cookieStore 设置为默认的 CookieStore。接下来,我们创建了一个 HttpGet 请求,并使用 httpClient 执行该请求以获取响应。最后,我们关闭了响应和 HttpClient。
您可以根据自己的需求修改代码,并根据需要添加其他请求参数和处理响应的逻辑。
用CloseableHttpClient写一个httpUtil请求类
好的,下面是一个用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实例,以释放资源。