HttpClient

一:HttpClient介绍:

HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

就是一个可以在java代码中发送http请求的工具包。

核心API:

  • HttpClient
  • HttpClients
  • CloseableHttpClient
  • HttpGet HttpPost

发送请求步骤:

  • 创建HttpClient对象
  • 创建Http请求对象
  • 调用HttpClient的execute方法发送请求

二、使用步骤:

1:引入maven坐标:

<dependency>    
    <groupId>org.apache.httpcomponents</groupId>        
    <artifactId>httpclient</artifactId>    
<version>4.5.13</version></dependency>

2:get请求案例:

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.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;

import java.io.IOException;

@SpringBootTest
public class HttpClient {
    /*
    httpclient 发起get请求
     */
    @Test
    public void TestHttpClientGet() throws IOException {
        //创建httpclient对象
        final CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建请求对象
        final HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        //发送请求,接收响应结果
        final CloseableHttpResponse response = httpClient.execute(httpGet);

        /*
        解析响应结果
        获取服务端返回的状态码
        获取服务端返回的数据(响应体)
         */
        final int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务端返回的状态码:"+statusCode);

        final HttpEntity entity = response.getEntity();
        //调用工具类进行解析
        final String body = EntityUtils.toString(entity);
        System.out.println("服务端返回的数据:"+body);

        //关闭资源
        response.close();
        httpClient.close();
    }
}

 这个测试就是一个get方式的请求,请求路径就是我们刚刚在用户端接口写的那个用户端状态查询的接口。

总体就是做了以下几步

  • 创建HttpClient对象
  • 创建Http请求对象
  • 调用HttpClient的execute方法发送请求
  • 解析了返回的实体entity(调用了一个EntityUtils的方法)
  • 关闭资源

我们进行测试的时候,一定得把服务跑起来,然后redis一定要打开,还有就是要设置一下店铺的状态(可以在前端页面中设置)

要不然就会报500 ,或者返回的结果data那个地方是null。

3:post请求案例:

    /*
    httpclient 发送post请求
     */
    @Test
    public void TestHttpClientPost() throws Exception{
        //创建httpclient对象
        final CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建请求对象
        final HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        final StringEntity stringEntity = new StringEntity(jsonObject.toString());
        //指定编码方式
        stringEntity.setContentEncoding("utf-8");
        //指定数据格式
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);

        //发送请求
        final CloseableHttpResponse response = httpClient.execute(httpPost);
        //解析返回结果。
        final int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        System.out.println("服务端返回的状态码:"+statusCode);
        //获得返回结果的实体
        final HttpEntity entity = response.getEntity();
        final String body = EntityUtils.toString(entity);
        System.out.println("服务端返回的数据:"+body);
        //关闭资源
        response.close();
        httpClient.close();
    }

POST请求方式需要传JSON格式的参数

  • 所以在创建请求对象这一步的时候,需要设置一下这个请求参数
  • 利用了JSONObject对象创建一个json格式的数据,并用toString()方法转成String类型的数据传入请求对象的实体。
  • 并且需要指定编码格式和数据格式
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值