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

Spring Boot调用api delete接口

时间:2022-08-20 16:25:09浏览次数:119  
标签:log Spring Boot client api httpDelete response delete

 Spring Boot调用api delete接口

示例:

   /**
     * 调用api delete接口
     *
     * @param url
     * @author li.zhm
     * @date 2022/8/20 14:07
     * @history <author>  <time>  <version>  <desc>
     */
    public static void sendDelete(String url) {
        log.info("请求delete地址:{}", url);
        String body = "";
        // 创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        // 创建delete方式请求对象
        HttpDelete httpDelete = new HttpDelete(url);
        RequestConfig requestConfig =
                RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
        httpDelete.setConfig(requestConfig);
        // 设置header信息, 指定报文头【Content-type】 【User-Agent】
        httpDelete.setHeader("Content-type", "application/json");
        httpDelete.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        try {
            // 执行请求操作,并拿到结果(同步阻塞)
            response = client.execute(httpDelete);
            log.info("response 返回状态码:{}", response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info("AMF取消订阅成功!");
            } else {
                log.error("response 返回状态码异常:{}", response.getStatusLine().getStatusCode());
            }
            // 释放链接
            response.close();
        } catch (IOException e) {
            log.error("调用amf-delete 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();
                }
            }
        }
    }

 

标签:log,Spring,Boot,client,api,httpDelete,response,delete
From: https://www.cnblogs.com/lizm166/p/16607958.html

相关文章

  • spring源码学习笔记1——解析xml生成BeanDefinition的过程解析
    spring源码学习笔记1——解析xml生成BeanDefinition的过程解析一丶Spring解析Xml生成BeanDefinition的流程1.指定xml路径解析xml首先需要知道xml的位置,如下我们构造了Ap......
  • 【Spring5学习笔记(4)】事务管理:
    事务1、什么是事务(1)事务是数据库操作的最基本单元,是逻辑上的一组操作,要么都成功,如果有一个失败则所有操作都失败(2)经典场景:银行转账2、事务的四个特性(ACID)(1)原子性:一组逻辑操......
  • SpringBoot 搭建和使用图形化监控界面
    我们开发好的SpringBoot服务发布到公网上,肯定希望能够对其状态和资源消耗情况进行监控,特别是对每个接口访问情况的统计,以便在发生问题时能够快速排查和分析并解决问题。......
  • ufw开机不启动,ufw inactive after reboot
    废话:最近部署debian服务器,我用ufw把ssh端口打开了,重启服务器后,ssh又连接不上了。我心想ufw开机不自启动的吗?那我服务器每次重启后我都得把显示器接上手动打开22端口,这不是......
  • webAPI与winform之间的数据交互
    首先是json的数据转化形式:这里将对象转化成json形式,后续发给服务器接收:stringjson=JsonConvert.SerializeObject(cabinetAdd);winform把接收到的json字符串,转回本来......
  • 聊聊@SpringBootApplication注解
    @SpringBootApplication其实就是以下三个注解的总和@Configuration: 用于定义一个配置类@EnableAutoConfiguration :SpringBoot会自动根据你jar包的依赖来自动配置项......
  • Spring源码-xml解析
    Spring使用SAX解析xml。SAX的全称是SimpleAPIsforXML,也即XML简单应用程序接口。与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式。当使用SAX分......
  • Spring Boot调用api post接口
    SpringBoot调用apipost接口示例publicstaticStringsendPost(Stringurl,JSONObjectjsonObject){log.info("请求地址:{}",url);Stringbody......
  • Spring 03: 基于xml的构造方法注入
    注入方式具体有3种注入方式:通过构造方法的a.参数名称注入b.参数下标注入c.默认参数顺序注入参数名称注入School实体类packagecom.example.pojo03;public......
  • 基于SpringBoot的SSMP整合
    前言实体类开发————使用Lombok快速制作实体类Mapper开发————整合MyBatisPlus,制作数据层测试Service开发————基于MyBatisPlus进行增量开发,制作业务层测试......