ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Boot调用api post接口

2022-08-20 14:02:44  阅读:168  来源: 互联网

标签: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
来源: https://www.cnblogs.com/lizm166/p/16607603.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有