• HttpClient实现RPC解析(通过Get方式访问)


    一 HttpClient简介

    在JDK中java.net包下提供了用户HTTP访问的基本功能,但是它缺少灵活性或许多应用所需要的功能。

    HttpClient起初是Apache Jakarta Common 的子项目。用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本。2007年成为顶级项目。

    通俗解释:HttpClient可以实现使用Java代码完成标准HTTP请求及响应。

    二 代码实现

    2.1服务端

    新建项目HttpClientServer
    在这里插入图片描述

    依赖

    <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.11.RELEASEversion>
        parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    新建控制器

    @Controller
    public class DemoController {
    
        @RequestMapping("/demo")
        @ResponseBody
        public String demo(String param){
            return param + "yqq";
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.2客户端

    新建HttpClientDemo项目
    在这里插入图片描述

    添加依赖

    <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.11.RELEASEversion>
        parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.apache.httpcomponentsgroupId>
                <artifactId>httpclientartifactId>
                <version>4.5.10version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    新建类

    public class HttpClientDemo {
    
        @Test
        public void testGetDemo(){
            //1.创建http工具(类似浏览器)发送请求,解析相应
            CloseableHttpClient httpClient = HttpClients.createDefault();
            try {
                //2.请求路劲
                URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/demo");
                uriBuilder.addParameter("param","yn");
                //3.创建httpGet请求对象
                HttpGet get = new HttpGet(uriBuilder.build());
                //4.创建相应对象
                CloseableHttpResponse httpResponse = httpClient.execute(get);
                //由于响应体是字符串,因此把HttpEntity类型转换为字符串,并设置编码字符集
                String result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                //输出结果
                System.out.println(result);
                //释放资源
                httpResponse.close();
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    测试

    在这里插入图片描述

  • 相关阅读:
    点大商城V2_2.5.0 全开源独立版 商家自营+多商户入驻 百度+支付宝+QQ+头条+小程序端+unipp开源前端
    【kafka】mac环境安装kafka
    纯干货:解读输出文件 | VASP零基础实用教程
    Redis哨兵模式详解
    House of pig 原理详解&实战
    【vue】使用less报错:显示this.getOptions is not a function
    网络套接字编程(二)
    基于TextRank算法生成文本摘要有代码+数据+可直接运行
    为什么要有redo log
    【探花交友】保存用户信息、上传用户头像、用户信息管理
  • 原文地址:https://blog.csdn.net/manba_yqq/article/details/126356388