HttpRequestUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright
  3. * All right reserved.
  4. * 项目名称:
  5. * 创建日期: 2022/11/9
  6. */
  7. package org.springblade.utils;
  8. import cn.hutool.core.util.CharsetUtil;
  9. import cn.hutool.http.ContentType;
  10. import cn.hutool.http.HttpRequest;
  11. import cn.hutool.http.HttpResponse;
  12. import cn.hutool.http.HttpStatus;
  13. import com.alibaba.fastjson.JSONObject;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springblade.core.log.exception.ServiceException;
  17. import java.util.Map;
  18. /**
  19. * 创建日期: 2022/11/9
  20. * Title: 文件所属模块(必须填写)
  21. * Description:对本文件的详细描述,原则上不能少于30字
  22. * @author DSH
  23. * @mender:(文件的修改者,文件创建者之外的人)
  24. * @version 1.0
  25. * Remark:认为有必要的其他信息
  26. */
  27. @Slf4j
  28. public class HttpRequestUtil {
  29. /**
  30. * 功能: GET请求
  31. * 作者: DSH
  32. * 创建日期: 2022/11/09
  33. * @param interfaceName 接口名称
  34. * @param url 接口地址
  35. * @param headers 请求头
  36. * @param paramMap 接口入参
  37. * @param nTimeOut 超时时间,单位:毫秒
  38. * @return: com.alibaba.fastjson.JSONObject
  39. */
  40. public static JSONObject doHttpGetRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
  41. log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
  42. HttpResponse response = HttpRequest.get(url)
  43. .form(paramMap)
  44. .addHeaders(headers)
  45. .timeout(nTimeOut)
  46. .execute();
  47. if (response.getStatus() == HttpStatus.HTTP_OK) {
  48. String body = response.body();
  49. log.info("【{}】接口响应:{}", interfaceName, body);
  50. if (StringUtils.isNotBlank(body)) {
  51. JSONObject result = JSONObject.parseObject(body);
  52. if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
  53. throw new ServiceException(result.getString("msg"));
  54. }
  55. return result;
  56. }
  57. } else {
  58. log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
  59. throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
  60. }
  61. return null;
  62. }
  63. /**
  64. * 功能: POST请求
  65. * 作者: DSH
  66. * 创建日期: 2022/11/09
  67. * @param interfaceName 接口名称
  68. * @param url 接口地址
  69. * @param headers 请求头
  70. * @param paramMap 接口入参
  71. * @param nTimeOut 超时时间,单位:毫秒
  72. * @return: com.alibaba.fastjson.JSONObject
  73. */
  74. public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
  75. log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
  76. HttpResponse response = HttpRequest.post(url)
  77. //.contentType("application/json;charset=utf-8")
  78. .contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
  79. .body(paramMap == null ? null : JSONObject.toJSONString(paramMap))
  80. .addHeaders(headers)
  81. .timeout(nTimeOut)
  82. .execute();
  83. if (response.getStatus() == HttpStatus.HTTP_OK) {
  84. String body = response.body();
  85. log.info("【{}】接口响应:{}", interfaceName, body);
  86. if (StringUtils.isNotBlank(body)) {
  87. JSONObject result = JSONObject.parseObject(body);
  88. if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
  89. throw new ServiceException(result.getString("msg"));
  90. }
  91. return result;
  92. }
  93. } else {
  94. log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
  95. throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
  96. }
  97. return null;
  98. }
  99. /**
  100. * POST请求 ,按JSON字符串传入BODY
  101. * @param interfaceName
  102. * @param url
  103. * @param headers
  104. * @param paramMap
  105. * @param nTimeOut
  106. * @return
  107. */
  108. public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, String paramMap, int nTimeOut) {
  109. log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
  110. HttpResponse response = HttpRequest.post(url)
  111. // .contentType("application/json;charset=utf-8")
  112. .contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
  113. .body(paramMap)
  114. .addHeaders(headers)
  115. .timeout(nTimeOut)
  116. .execute();
  117. if (response.getStatus() == HttpStatus.HTTP_OK) {
  118. String body = response.body();
  119. log.info("【{}】接口响应:{}", interfaceName, body);
  120. if (StringUtils.isNotBlank(body)) {
  121. JSONObject result = JSONObject.parseObject(body);
  122. // if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
  123. // throw new ServiceException(result.getString("msg"));
  124. // }
  125. return result;
  126. }
  127. } else {
  128. log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
  129. throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
  130. }
  131. return null;
  132. }
  133. /**
  134. * 功能: POST请求
  135. * 作者: DSH
  136. * 创建日期: 2022/11/09
  137. * @param interfaceName 接口名称
  138. * @param url 接口地址
  139. * @param headers 请求头
  140. * @param paramMap 接口入参
  141. * @param nTimeOut 超时时间,单位:毫秒
  142. * @return: com.alibaba.fastjson.JSONObject
  143. */
  144. public static JSONObject doHttpPostSubmitForm(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
  145. log.info("【{}】接口地址:{},参数:{}", interfaceName, url, paramMap);
  146. HttpResponse response = HttpRequest.post(url)
  147. .form(paramMap)
  148. .addHeaders(headers)
  149. .timeout(nTimeOut)
  150. .execute();
  151. if (response.getStatus() == HttpStatus.HTTP_OK) {
  152. String body = response.body();
  153. log.info("【{}】接口响应:{}", interfaceName, body);
  154. if (StringUtils.isNotBlank(body)) {
  155. JSONObject result = JSONObject.parseObject(body);
  156. if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
  157. throw new ServiceException(result.getString("msg"));
  158. }
  159. return result;
  160. }
  161. } else {
  162. log.error("【{}】请求异常,Http状态码:{}", interfaceName, response.getStatus());
  163. throw new ServiceException("请求异常,Http状态码:"+ response.getStatus());
  164. }
  165. return null;
  166. }
  167. }