• HttpUtil


    1. package com.cmb.utils;
    2. import com.fasterxml.jackson.databind.ObjectMapper;
    3. import org.apache.http.Header;
    4. import org.apache.http.HttpEntity;
    5. import org.apache.http.HttpHeaders;
    6. import org.apache.http.client.config.RequestConfig;
    7. import org.apache.http.client.methods.CloseableHttpResponse;
    8. import org.apache.http.client.methods.HttpGet;
    9. import org.apache.http.client.methods.HttpPost;
    10. import org.apache.http.entity.ContentType;
    11. import org.apache.http.entity.StringEntity;
    12. import org.apache.http.impl.client.CloseableHttpClient;
    13. import org.apache.http.impl.client.HttpClients;
    14. import org.apache.http.message.BasicHeader;
    15. import org.junit.rules.Timeout;
    16. import org.springframework.stereotype.Component;
    17. import javax.annotation.PostConstruct;
    18. import java.io.IOException;
    19. import java.util.ArrayList;
    20. import java.util.HashMap;
    21. import java.util.List;
    22. import java.util.Map;
    23. import java.util.concurrent.TimeUnit;
    24. @Component
    25. public class HttpUtil {
    26. private CloseableHttpClient httpClient;
    27. private static RequestConfig requestConfig;
    28. private static final int HTTP_CONNECTION_TIMEOUT_DEFAULT = 2000;
    29. private static final int HTTP_RESPONSE_TIMEOUT_DEFAULT = 2000;
    30. private static List
      headers;
    31. static{
    32. requestConfig = RequestConfig.custom()
    33. //设置连接超时时间
    34. .setConnectTimeout(HTTP_CONNECTION_TIMEOUT_DEFAULT)
    35. //设置相应的超时时间
    36. .setSocketTimeout(HTTP_RESPONSE_TIMEOUT_DEFAULT)
    37. .build();
    38. headers = new ArrayList<>();
    39. headers.add(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=utf-8"));
    40. headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, x-gzip, deflate"));
    41. headers.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
    42. }
    43. @PostConstruct
    44. public void init(){
    45. // httpClient = HttpClients.createDefault();
    46. httpClient = HttpClients.custom()
    47. .setDefaultHeaders(headers)
    48. .setDefaultRequestConfig(requestConfig)
    49. .build();
    50. }
    51. public CloseableHttpResponse getRequest(String uri) throws IOException {
    52. HttpGet httpGet = new HttpGet(uri);
    53. CloseableHttpResponse response = httpClient.execute(httpGet);
    54. return response;
    55. }
    56. public CloseableHttpResponse postRequest(String uri,Object javaBean) throws IOException {
    57. HttpPost httpPost = new HttpPost(uri);
    58. ObjectMapper mapper = new ObjectMapper();
    59. String jsonStr = mapper.writeValueAsString(javaBean);
    60. StringEntity entity = new StringEntity(jsonStr);
    61. httpPost.setEntity(entity);
    62. CloseableHttpResponse response = httpClient.execute(httpPost);
    63. return response;
    64. }
    65. public static void main(String[] args) {
    66. Map map = new HashMap<>();
    67. map.put("name","wfl");
    68. String uri = "http://127.0.0.1:8080/test/post";
    69. try{
    70. HttpUtil httpUtil = new HttpUtil();
    71. CloseableHttpResponse response = httpUtil.postRequest(uri,map);
    72. if("200".equals(response.getStatusLine())){
    73. System.out.println("请求成功");
    74. HttpEntity httpEntity = response.getEntity();
    75. System.out.println("相应内容为"+httpEntity.getContent());
    76. }
    77. }catch (Exception e){
    78. System.out.println("发送post请求时异常");
    79. }
    80. }
    81. }

    http工具类

  • 相关阅读:
    你是否还迷茫要不要学习Linux?
    springboot小商户茶叶信息管理毕业设计-附源码
    计算机毕业设计django基于python精品课程在线学习系统(源码+系统+mysql数据库+Lw文档)
    nginx修改配置文件不生效
    Vue3中runtime-dom的实现-详细步骤
    commons-collections4工具常用方法
    DNS抓包
    修timing中的SI-noise问题
    【LeetCode】412.Fizz Buzz
    MAUI Blazor 项目实战 - 从0到1轻松构建多平台应用UI
  • 原文地址:https://blog.csdn.net/fulong0406/article/details/126693607