本文最后更新于98 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
介绍
HTTP客户端是APACHE Jakarta Common下的子项目,可以用来提供高效的、最新的、功能丰富的支持http协议
的客户端编程工具包,并且它支持Http协议最新的版本和建议。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
核心AP1:
- HttpClient
- HttpClients
- CloseableHttpClient
- HttpGet
- HttpPost
发送请求步骤
- 创建HttpClient对象
- 创建Http请求对象
- 调用HttpClient的execute方法发送请求
入门案例
导入坐标
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
创建测试类 HttpClientTest
@SpringBootTest
public class HttpClientTest {
}
创建测试通过httpclient发送GET请求
// 1.创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();// 2.创建HttpGet对象,设置请求地址
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");// 3.使用HttpClient对象发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpGet);// 4.打印响应状态码
System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String boby = EntityUtils.toString(entity);
// 打印响应内容
System.out.println("响应内容为:" + boby);// 最后关闭HttpClient和HttpResponse对象
response.close();
httpClient.close();
完整代码
/**
* 测试通过httpclient发送GET请求
*/
@Test
public void testGet() throws Exception {
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpGet对象,设置请求地址
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
// 使用HttpClient对象发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpGet);
// 打印响应状态码
System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String boby = EntityUtils.toString(entity);
// 打印响应内容
System.out.println("响应内容为:" + boby);
// 关闭HttpClient和HttpResponse对象
response.close();
httpClient.close();
}
运行测试结果
前提项目已经启动
创建测试通过httpclient发送POST请求
// 1.创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.创建HttpPost对象,设置请求地址和请求参数
HttpPost httpPOST = new HttpPost("http://localhost:8080/admin/employee/login");
// 3.设置请求参数JSON
JSONObject json = new JSONObject();
json.put("username", "admin");
json.put("password", "123456");
StringEntity entity = new StringEntity(json.toString());//4.指定请求内容类型为json
entity.setContentType("application/json");//5.指定请求参数编码格式
entity.setContentEncoding("UTF-8");//6.设置请求参数
httpPOST.setEntity(entity);// 7.使用HttpClient对象发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpPOST);// 8.打印响应状态码
System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);// 9.打印响应内容
System.out.println("响应内容为:" + responseBody);// 10.关闭HttpClient和HttpResponse对象
response.close();
httpClient.close();
完整代码
/**
* 测试通过httpclient发送POST请求
*/
@Test
public void testPOST() throws Exception {
// 1.创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.创建HttpPost对象,设置请求地址和请求参数
HttpPost httpPOST = new HttpPost("http://localhost:8080/admin/employee/login");
// 3.设置请求参数
JSONObject json = new JSONObject();
json.put("username", "admin");
json.put("password", "123456");
StringEntity entity = new StringEntity(json.toString());
//4.指定请求内容类型为json
entity.setContentType("application/json");
//5.指定请求参数编码格式
entity.setContentEncoding("UTF-8");
//6.设置请求参数
httpPOST.setEntity(entity);
// 7.使用HttpClient对象发送请求,并获取响应
CloseableHttpResponse response = httpClient.execute(httpPOST);
// 8.打印响应状态码
System.out.println("状态码为:" + response.getStatusLine().getStatusCode());
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);
// 9.打印响应内容
System.out.println("响应内容为:" + responseBody);
// 10.关闭HttpClient和HttpResponse对象
response.close();
httpClient.close();
}
运行测试结果
前提项目已经启动