当前位置: 首页 > article >正文

苍穹外卖学习笔记(十一)

一. HttpClient

介绍

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

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

该项目导入了阿里的sdk,传递依赖
核心API:

  1. HttpClient
  2. HttpClients
  3. CloseableHttpClient
  4. HttpGet
  5. HttpPost

发生请求步骤:

  1. 创建HttpClient对象
  2. 创建Http请求对象
  3. 调用HttpClient的execute方法发送请求

入门案例

HttpClientTest.java

package com.sky.test;

import com.alibaba.fastjson.JSONObject;
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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class HttpClientTest {

    /**
     * 测试HttpClient发送get请求
     */
    @Test
    public void testGet() {
        // 创建HttpClient对象
        CloseableHttpClient aDefault = HttpClients.createDefault();
        // 创建HttpGet对象
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        // 发送请求
        try {
            CloseableHttpResponse execute = aDefault.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();
            aDefault.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 测试HttpClient发送post请求
     */
    @Test
    public void testPost() throws Exception {
        // 创建HttpClient对象
        CloseableHttpClient aDefault = HttpClients.createDefault();
        // 创建HttpPost对象
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", "admin");
        jsonObject.put("password", "123456");
        StringEntity entity = new StringEntity(jsonObject.toString());
        // 设置请求头
        entity.setContentEncoding("UTF-8");
        // 设置请求体
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        // 发送请求
        CloseableHttpResponse execute = aDefault.execute(httpPost);
        // 获取响应数据
        int statusCode = execute.getStatusLine().getStatusCode();
        System.out.println("状态码:" + statusCode);
        HttpEntity entity1 = execute.getEntity();
        String string = EntityUtils.toString(entity1);
        System.out.println("响应数据:" + string);

        // 关闭资源
        execute.close();
        aDefault.close();
    }
}

http://www.kler.cn/news/318842.html

相关文章:

  • 智谱AI:CogVideoX-2b——视频生成模型的得力工具
  • 短视频矩阵源码/短视频矩阵系统搭建/源码开发知识分享
  • Github + Hexo + Shoka搭建个人博客以及遇到的部分问题
  • mysqldump使用cmd窗口和powersell窗口导出sql中文乱码的问题
  • 工厂模式在短信发送中的应用 —— 以腾讯云、阿里云、华为云为例
  • Vue.js 中,@click 和 @click.stop的区别
  • Redis 多级缓存
  • 可以把台式电脑做成服务器吗
  • 状态模式原理剖析
  • OpenCV normalize() 函数详解及用法示例
  • 钰泰-ETA6964A 锂电池充电器IC
  • 基于STM32F103C8T6单片机的农业环境监测系统设计
  • 3D模型在UI设计中应用越来越多,给UI带来了什么?
  • API代理是什么?解读其原理与作用
  • golang context管理channel
  • 【数据库】sqlite
  • 【C++】托管类和托管函数
  • 大模型备案和互联网算法备案的区别?
  • Linux软件包管理器、Linux开发工具、vim的配置等的介绍
  • RabbitMQ简介
  • AI产品经理必知的133个专业术语
  • 高阶函数(Higher-Order Function)
  • 高侧电流检测电路设计
  • [新闻]Tom Sawyer Software宣布发布SysML v2 Viewer
  • GLM-4-9B 是智谱 AI 推出的预训练模型 GLM-4 系列中的开源版本
  • Pytest-allure如何在测试完成后自动生成完整报告?
  • 占领矩阵-第15届蓝桥省赛Scratch中级组真题第5题
  • 桥梁轻量化结构监测系统解决方案
  • [OPEN SQL] SELECT语句
  • Android JNI 调用流程