• Java 发送 Http 请求工具类(HttpClient)


    1. import com.gientech.exception.BusinessException;
    2. import com.gientech.util.LoggerUtil;
    3. import org.apache.http.HttpEntity;
    4. import org.apache.http.HttpResponse;
    5. import org.apache.http.ParseException;
    6. import org.apache.http.client.methods.HttpGet;
    7. import org.apache.http.client.methods.HttpPost;
    8. import org.apache.http.entity.ContentType;
    9. import org.apache.http.entity.StringEntity;
    10. import org.apache.http.impl.client.CloseableHttpClient;
    11. import org.apache.http.impl.client.HttpClientBuilder;
    12. import org.apache.http.util.EntityUtils;
    13. import org.slf4j.Logger;
    14. import org.slf4j.LoggerFactory;
    15. import org.springframework.beans.factory.annotation.Autowired;
    16. import java.io.IOException;
    17. import java.nio.charset.Charset;
    18. import java.nio.charset.UnsupportedCharsetException;
    19. /**
    20. *
    21. * HttpClient
    22. * @author xiarg
    23. * @date 2023/11/1 17:18
    24. */
    25. public class TdspHttpClient {
    26. private static final Logger logger = LoggerFactory.getLogger(TdspHttpClient.class);
    27. @Autowired
    28. private static CloseableHttpClient httpClient;
    29. static {
    30. httpClient = HttpClientBuilder.create().build();
    31. }
    32. /**
    33. *
    34. * http的get请求
    35. * @param url 请求路径
    36. * @return null
    37. * @author xiarg
    38. * @date 2023/11/2 13:35
    39. */
    40. public static String sendGetRequest(String url,String responseCharset) throws Exception{
    41. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>接口请求start<<<<<===== {0}", url);
    42. HttpEntity entity = null;
    43. String result = null;
    44. try {
    45. HttpGet httpGet = new HttpGet(url);
    46. HttpResponse response = httpClient.execute(httpGet);
    47. return handleResponse( response, responseCharset);
    48. } catch (IOException e) {
    49. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>发送请求出现异常<<<<<===== {0}",
    50. e.getMessage());
    51. e.printStackTrace();
    52. } catch (ParseException e) {
    53. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>发送请求出现异常<<<<<===== {0}",
    54. e.getMessage());
    55. e.printStackTrace();
    56. } finally {
    57. try {
    58. EntityUtils.consume(entity);
    59. } catch (IOException e) {
    60. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>清理缓存出现异常<<<<<===== {0}",
    61. e.getMessage());
    62. e.printStackTrace();
    63. }
    64. }
    65. return result;
    66. }
    67. /**
    68. *
    69. * http的post秦秋
    70. * @param url
    71. * @param requestBody
    72. * @param contentType 媒体类型
    73. * @param entryCharset 请求报文编码
    74. * @param responseCharset 响应报文编码
    75. * @return null
    76. * @author xiarg
    77. * @date 2023/11/2 13:36
    78. */
    79. public static String sendPostRequest(String url, String requestBody, String contentType ,String entryCharset,
    80. String responseCharset) throws Exception{
    81. LoggerUtil.info(logger, "[HttpClientUtils.sendPostRequest]=====>>>>>接口请求start<<<<<===== {0}", url);
    82. HttpEntity entity = null;
    83. String result = null;
    84. try {
    85. HttpPost httpPost = new HttpPost(url);
    86. ContentType entryContentType = ContentType.create(contentType, Charset.forName(entryCharset));
    87. httpPost.setEntity(new StringEntity(requestBody, entryContentType));
    88. HttpResponse response = httpClient.execute(httpPost);
    89. return handleResponse(response,responseCharset);
    90. } catch (UnsupportedCharsetException e) {
    91. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>发送请求出现异常<<<<<===== {0}",
    92. e.getMessage());
    93. e.printStackTrace();
    94. } catch (IOException e) {
    95. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>发送请求出现异常<<<<<===== {0}",
    96. e.getMessage());
    97. e.printStackTrace();
    98. } catch (ParseException e) {
    99. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>发送请求出现异常<<<<<===== {0}",
    100. e.getMessage());
    101. e.printStackTrace();
    102. } finally {
    103. try {
    104. EntityUtils.consume(entity);
    105. } catch (IOException e) {
    106. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>清理缓存出现异常<<<<<===== {0}",
    107. e.getMessage());
    108. e.printStackTrace();
    109. }
    110. }
    111. return result;
    112. }
    113. private static String handleResponse(HttpResponse response,String responseCharset) throws IOException,BusinessException{
    114. int statusCode = response.getStatusLine().getStatusCode();
    115. if (statusCode >= 200 && statusCode < 300) {
    116. HttpEntity entity = response.getEntity();
    117. String result = EntityUtils.toString(entity,Charset.forName(responseCharset));
    118. LoggerUtil.info(logger, "[HttpClientUtils.sendGetRequest]=====>>>>>接口请求返回结果<<<<<===== {0}",
    119. result);
    120. return result;
    121. }
    122. throw new BusinessException("调用接口出现异常!");
    123. }
    124. }

            此工具类可以直接复制使用。

  • 相关阅读:
    【Java面试小短文】Spring 如何解决循环依赖?
    Java ByteArrayOutputStream类简介说明
    Java之异常浅析
    uni-app 之 web-view 与h5 通讯
    3D Web轻量化引擎HOOPS Communicator如何优化AEC领域3D大模型体验?
    AI前沿-YOLOV9算法
    黑马React18: 基础Part 1
    我的Quick Latex For Obsidian-Setting(持续更新)
    今天的消费情况
    ubuntu 复制文件路径
  • 原文地址:https://blog.csdn.net/m0_65014849/article/details/134179116