首页 > 其他分享 >Spring Boot调用api post接口

Spring Boot调用api post接口

时间:2022-08-20 13:55:16浏览次数:104  
标签:body log Spring Boot client api httpPost entity response

Spring Boot调用api post接口

示例

public static String sendPost(String url, JSONObject jsonObject) {

        log.info("请求地址:{}", url);
        String body = "";
        // 创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();

        CloseableHttpResponse response = null;
        // 创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        // 装填参数
        StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        // 设置参数到请求对象中
        httpPost.setEntity(s);
        log.info("请求参数:{}", jsonObject.toString());

        // 设置header信息, 指定报文头【Content-type】 【User-Agent】
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        try {
            // 执行请求操作,并拿到结果(同步阻塞)
            response = client.execute(httpPost);
            log.info("response 返回状态码:{}", response.getStatusLine().getStatusCode());
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 获取结果实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // 按指定编码转换结果实体为String类型
                    body = EntityUtils.toString(entity, "utf-8");
                    log.info("响应body:", body);
                }
                // 关闭流
                EntityUtils.consume(entity);
            }else{
                log.error("response 返回状态码异常:{}", response.getStatusLine().getStatusCode());
            }
            // 释放链接
            response.close();
        } catch (IOException e) {
            log.error("调用amf-sub api异常,{0}", e);
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    log.error("关闭http client异常,{0}", e);
                    e.printStackTrace();
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    log.error("关闭http response异常,{0}", e);
                    e.printStackTrace();
                }
            }
        }
        return body;
    }

pom

<!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!-- fastjson End -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

 

标签:body,log,Spring,Boot,client,api,httpPost,entity,response
From: https://www.cnblogs.com/lizm166/p/16607603.html

相关文章

  • Spring 03: 基于xml的构造方法注入
    注入方式具体有3种注入方式:通过构造方法的a.参数名称注入b.参数下标注入c.默认参数顺序注入参数名称注入School实体类packagecom.example.pojo03;public......
  • 基于SpringBoot的SSMP整合
    前言实体类开发————使用Lombok快速制作实体类Mapper开发————整合MyBatisPlus,制作数据层测试Service开发————基于MyBatisPlus进行增量开发,制作业务层测试......
  • 如何在Spring Boot Rest服务方法中设置响应头值
    如何在SpringBootRest服务方法中设置响应头值问题描述新手问题...我正在构建我的第一个SpringBootRestful服务.我的静态服务设计要求在响应头中返回一些数据. 推荐......
  • 通过Spring官网创建Spring Boot项目
    spring官网:https://start.spring.io/1、首先,在官网创建springboot项目:2、创建完毕后单击“GENERATE”网页会自动下载项目压缩包:3、解压demo.zip文件夹:4、打开IDEA......
  • SpringBoot整合Redis实现常用功能
    SpringBoot整合Redis实现常用功能建议大小伙们,在写业务的时候,提前画好流程图,思路会清晰很多。文末有解决缓存穿透和击穿的通用工具类。1登陆功能我想,登陆功能......
  • IDEA初始化创建SpringBoot项目
    创建SpringBoot打开新建项目配置项目基本需求其中URL改成:start.springboot.io创建完成后等待依赖包下载下载完成之后点击右侧Maven中的Lifecycle-install进行更新......
  • Spring 02: Spring接管下的三层项目架构
    业务背景需求:使用三层架构开发,将用户信息导入到数据库中目标:初步熟悉三层架构开发核心操作:开发两套项目,对比Spring接管下的三层项目构建和传统三层项目构建的区别注意......
  • SpringBoot中调用Kafka
    Kafka实战——在SpringBoot中的应用官网文档链接1.pom引用 <dependency><groupId>org.springframework.kafka</groupId><artifactId>spri......
  • Spring Security登录的流程
    SpringSecurity登录的流程1、UsernamePasswordAuthenticationFilter这过滤器开始attemptAuthentication方法请求的request中的参数setDetails(request,authReque......
  • spring boot thymeleaf 不能访问templates目录下的页面问题
    springboot默认情况下可以直接访问四个目录下的静态文件(https://www.cnblogs.com/realzhaijiayu/p/16566667.html)publicstaticresourcesMETA-INF/resources引入thym......