一、简介
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
二、使用
使用HttpClient可发送HTTP请求、接收响应数据
应用场景
当我们在使用扫描支付、查看地图、获取验证码、查看天气等功能时,应用程序(后台Java程序)本身并未实现这些功能,都是在应用程序里访问提供这些功能的服务(客户端提供),访问这些服务需要发送HTTP请求,并且接收响应数据,可通过HttpClient来实现。
0.准备
在项目引入aliyun-sdk-oss依赖,该依赖底层包含了HttpClient的相关依赖
1.创建HTTPClient对象
//1.创建HTTP Client对象
CloseableHttpClient httpClient = HttpClients.createDefault();
2.创建Http请求对象
GET请求
//2.创建请求对象HttpGet
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
POST请求
//2.创建请求对象HttpPost
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
3.调用HttpClient的excute方法发送请求
//发送GET请求,接受响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
//发送POST请求,接受响应结果
CloseableHttpResponse response = httpClient.execute(httpPost);
案例
import com.alibaba.fastjson.JSON;
import com.sky.dto.EmployeeLoginDTO;
import org.apache.http.HttpEntity;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
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.ContentType;
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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ClientShopControllerTest {
@Test
public void getTest() throws Exception {
//1.创建HTTP Client对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.创建请求对象
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
//3。发送请求,接收响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
//4.解析结果
StatusLine statusLine = response.getStatusLine();
//4.1状态码
int statusCode = statusLine.getStatusCode();
//4.2状态文本
String reasonPhrase = statusLine.getReasonPhrase();
//4.3协议版本
ProtocolVersion protocolVersion = statusLine.getProtocolVersion();
System.out.println("状态码"+statusCode);
System.out.println("状态文本"+reasonPhrase);
System.out.println("协议版本"+protocolVersion);
//4.4响应体
HttpEntity httpEntity = response.getEntity();
//需要将json转化为string
String body = EntityUtils.toString(httpEntity);
System.out.println(body);
//关闭资源
response.close();
httpClient.close();
}
@Test
public void postTest() throws Exception {
//1.创建HTTP Client对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.创建请求对象
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
EmployeeLoginDTO employeeLoginDTO = new EmployeeLoginDTO();
employeeLoginDTO.setUsername("admin");
employeeLoginDTO.setPassword("123456");
String json = JSON.toJSONString(employeeLoginDTO);
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
//3。发送请求,接收响应结果
CloseableHttpResponse response = httpClient.execute(httpPost);
//4.解析结果
StatusLine statusLine = response.getStatusLine();
//4.1状态码
int statusCode = statusLine.getStatusCode();
//4.2状态文本
String reasonPhrase = statusLine.getReasonPhrase();
//4.3协议版本
ProtocolVersion protocolVersion = statusLine.getProtocolVersion();
System.out.println("状态码"+statusCode);
System.out.println("状态文本"+reasonPhrase);
System.out.println("协议版本"+protocolVersion);
//4.4响应体
HttpEntity httpEntity = response.getEntity();
//需要将json转化为string
String body = EntityUtils.toString(httpEntity);
System.out.println(body);
//关闭资源
response.close();
httpClient.close();
}
}