📅  最后修改于: 2023-12-03 14:59:20.477000             🧑  作者: Mango
Apache HttpClient是由Apache Software Foundation开发的、采用Java语言的HTTP客户端工具包。它可用于模拟发送HTTP请求并接收HTTP响应,支持HTTP/1.1和HTTP/2协议,可以轻松地创建、发送、接收和处理HTTP请求和响应。
本文将介绍使用Apache HttpClient发布HTTP请求的方法,包括创建HttpClient对象、创建HttpGet或HttpPost对象、设置参数、执行请求等步骤。
在使用Apache HttpClient之前,先确保Maven中已经引入相应的依赖。在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
HTTP GET请求通常用于获取资源,可以通过Apache HttpClient来发送HTTP GET请求。以下是示例代码:
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpGetExample {
public static void main(String[] args) throws Exception {
HttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.example.com");
httpGet.addHeader("User-Agent", "Mozilla/5.0");
String responseBody = httpclient.execute(httpGet, response -> EntityUtils.toString(response.getEntity()));
System.out.println(responseBody);
}
}
我们首先创建一个HttpClient对象并设置User-Agent头文件,然后创建HttpGet对象并设置URL,最后执行请求并获取响应体。
HTTP POST请求通常用于向服务器提交数据,可以通过Apache HttpClient来发送HTTP POST请求。以下是示例代码:
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpPostExample {
public static void main(String[] args) throws Exception {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://www.example.com");
httpPost.addHeader("User-Agent", "Mozilla/5.0");
StringEntity entity = new StringEntity("name=value");
httpPost.setEntity(entity);
String responseBody = httpclient.execute(httpPost, response -> EntityUtils.toString(response.getEntity()));
System.out.println(responseBody);
}
}
我们首先创建一个HttpClient对象并设置User-Agent头文件,然后创建HttpPost对象并设置URL和请求体,最后执行请求并获取响应体。
在使用Apache HttpClient时,我们可以通过HttpClientBuilder来设置HTTP客户端的参数。以下是示例代码:
import org.apache.http.client.HttpClient;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.HttpClientBuilder;
public class HttpClientConfig {
public static void main(String[] args) {
HttpClientBuilder builder = HttpClientBuilder.create();
SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(10000)
.build();
builder.setDefaultSocketConfig(socketConfig);
HttpClient httpClient = builder.build();
}
}
我们首先创建一个HttpClientBuilder对象然后设置SocketConfig,最后通过builder.build()方法创建HttpClient对象。
使用Apache HttpClient发送HTTP请求后,我们可以获取响应实体并进行处理。以下是示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.InputStream;
public class HttpEntityExample {
public static void main(String[] args) throws Exception {
HttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.example.com");
httpGet.addHeader("User-Agent", "Mozilla/5.0");
HttpEntity httpEntity = httpclient.execute(httpGet).getEntity();
InputStream inputStream = httpEntity.getContent();
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
// do something with the buffer
}
String responseBody = EntityUtils.toString(httpEntity);
System.out.println(responseBody);
}
}
我们首先创建一个HttpGet对象并执行请求,然后通过getEntity()方法获取响应实体,通过InputStream读取实体内容,最后通过EntityUtils.toString()方法获取实体字符串。
Apache HttpClient是一个强大的HTTP客户端工具包,可以帮助我们轻松地创建、发送、接收和处理HTTP请求和响应。本文介绍了使用Apache HttpClient发送HTTP GET和POST请求的方法,并提供了设置HTTP客户端参数和处理响应的示例代码。希望这篇文章对您有所帮助。