zoukankan      html  css  js  c++  java
  • Spring Boot 之httpClient使用

    版权声明:本文为博主原创文章,转载时请在文章最前方附上本文地址。 https://blog.csdn.net/qq_35033270/article/details/80112085


    超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。而HttpClient是可以支持http相关协议的工具包,它有如下功能:
    1.实现了所有的http方法(GET,POST,PUT,HEAD 等)
    2.支持自动转向
    3.支持 HTTPS 协议
    4.支持代理服务器等
    既然HttpClient使用这么广泛,则本文讲解下Spring Boot 中怎么使用HttpClient.如下:
    一.引入相关依赖

    <!-- http所需包 -->
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    </dependency>
    <!-- /http所需包 -->

    <!-- 数据解析所需包 -->
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
    </dependency>
    <dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.4</version>
    </dependency>
    <!-- /数据解析所需包 -->
    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
    28
    29
    30
    31
    32
    二.编写相关工具类
    写个http的工具类,以便业务代码直接调用,如下:

    /**
    * Http工具类
    */
    public class HttpUtils {

    /**
    * 发送POST请求
    * @param url 请求url
    * @param data 请求数据
    * @return 结果
    */
    @SuppressWarnings("deprecation")
    public static String doPost(String url, String data) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    RequestConfig requestConfig = RequestConfig.custom()
    .setSocketTimeout(10000).setConnectTimeout(20000)
    .setConnectionRequestTimeout(10000).build();
    httpPost.setConfig(requestConfig);
    String context = StringUtils.EMPTY;
    if (!StringUtils.isEmpty(data)) {
    StringEntity body = new StringEntity(data, "utf-8");
    httpPost.setEntity(body);
    }
    // 设置回调接口接收的消息头
    httpPost.addHeader("Content-Type", "application/json");
    CloseableHttpResponse response = null;
    try {
    response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    context = EntityUtils.toString(entity, HTTP.UTF_8);
    } catch (Exception e) {
    e.getStackTrace();
    } finally {
    try {
    response.close();
    httpPost.abort();
    httpClient.close();
    } catch (Exception e) {
    e.getStackTrace();
    }
    }
    return context;
    }

    /**
    * 解析出url参数中的键值对
    * @param url url参数
    * @return 键值对
    */
    public static Map<String, String> getRequestParam(String url) {

    Map<String, String> map = new HashMap<String, String>();
    String[] arrSplit = null;

    // 每个键值为一组
    arrSplit = url.split("[&]");
    for (String strSplit : arrSplit) {
    String[] arrSplitEqual = null;
    arrSplitEqual = strSplit.split("[=]");

    // 解析出键值
    if (arrSplitEqual.length > 1) {
    // 正确解析
    map.put(arrSplitEqual[0], arrSplitEqual[1]);
    } else {
    if (arrSplitEqual[0] != "") {
    map.put(arrSplitEqual[0], "");
    }
    }
    }
    return map;
    }
    }
    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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    三.业务代码中使用
    业务中代码使用,拼装请求Url和请求数据,就可以调用工具类里的doPost()方法开始直接使用咯。如下:

    private String getFileStorePath(String courtId, String seesionId){
    String fileStorePath = StringUtils.EMPTY;
    //请求参数
    String data = "{"courtId":"" + courtId + "","sessionId":"" + seesionId + ""}";
    String fileServiceUrl="http://111.11.11.11:8086";
    //发送请求,获取结果
    String result = HttpUtils.doPost(fileServiceUrl + "/ms-service/voice/search", data);
    if(StringUtils.isNotBlank(result)){
    com.alibaba.fastjson.JSONObject jsonobject = JSON.parseObject(result);
    fileStorePath = jsonobject.getString("path");
    logger.info("fileStorePath = " + fileStorePath);
    }
    return fileStorePath;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    如有不当之处,烦请斧正,一起沟通。

    原文:https://blog.csdn.net/qq_35033270/article/details/80112085

  • 相关阅读:
    bash中一次性给多个变量赋值命名管道的使用
    Mysql复制还原后root帐号权限丢失问题
    TC中HTB的使用备注
    Python 调用JS文件中的函数
    PIL图像处理模块,功能强大、简单易用(转)
    openfeign 实现动态Url
    Extjs 进度条的应用【转】
    Javascript 对象与数组中的函数下【转】
    Linq学习笔记之一:Linq To XML
    Sql Server查询语句的一些小技巧
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/10090788.html
Copyright © 2011-2022 走看看