HttpClient使用方法初解

一、简介

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();
​
    }
}
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值