HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
HttpClient的作用是发送HTTP请求和接收响应数据。
HttpClient的maven坐标
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
HttpClient的核心API
HttpClient:Http客户端对象类型,使用该类型对象可发起Http请求。
HttpClients:可认为是构建器,可创建HttpClient对象。
CloseableHttpClient:实现类,实现了HttpClient接口。
HttpGet:Get方式请求类型。
HttpPost:Post方式请求类型。
HttpClient的发送请求步骤
1.创建HttpClient对象
2.创建Http请求对象
3.调用HttpClient的execute方法发送请求
GET方式请求
步骤
1.创建HttpClient对象
2.创建请求对象
3.发送请求,接受响应结果
4.解析结果
5.关闭资源
package com.sky;
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 org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
public class HttpClientTest {
//GET方式
@Test
public void testGet() throws IOException {
//创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象 填写自己所需路径
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
//发送请求
CloseableHttpResponse execute = httpClient.execute(httpGet);
//获取响应的返回状态码
int statusCode = execute.getStatusLine().getStatusCode();
System.out.println("返回状态码:"+statusCode);
HttpEntity entity = execute.getEntity();
String string = EntityUtils.toString(entity);
System.out.println("服务端返回响应体:"+string);
//释放资源
execute.close();
httpClient.close();
}
POST方式请求
1.创建HttpClient对象
2.创建请求对象
3.发送请求,接收响应结果
4.解析响应结果
5.关闭资源
package com.sky;
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 org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
public class HttpClientTest {
//POST方式
@Test
public void testPost() throws Exception {
//创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象 填写自己所需路径
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
//组装参数
JSONObject jsonObject = new JSONObject();
jsonObject.put("username","admin");
jsonObject.put("password","123456");
StringEntity stringEntity = new StringEntity(jsonObject.toString());
//指定编码方式
stringEntity.setContentEncoding("utf-8");
//设置数据格式
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
//发送请求
CloseableHttpResponse execute = httpClient.execute(httpPost);
//获取响应的返回状态码
int statusCode = execute.getStatusLine().getStatusCode();
System.out.println("返回状态码:"+statusCode);
HttpEntity entity = execute.getEntity();
String string = EntityUtils.toString(entity);
System.out.println("服务端返回响应体:"+string);
//释放资源
execute.close();
httpClient.close();
}
}