UtilHttp.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.malk.utils;
  2. import cn.hutool.core.util.CharsetUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.XmlUtil;
  5. import cn.hutool.http.HttpRequest;
  6. import cn.hutool.http.HttpResponse;
  7. import cn.hutool.http.HttpUtil;
  8. import cn.hutool.http.webservice.SoapClient;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.serializer.SerializerFeature;
  11. import com.malk.server.common.VenR;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import java.io.File;
  15. import java.util.Map;
  16. /**
  17. * HttpUtil [取值详见CatchException]
  18. * -
  19. * - 后端方法说明
  20. * 1. param 使用转码, 避免中文字符导致参数识别异常, 如中文小括号
  21. * 2. body 注意 .form 是表单内容; .body 才是请求内容 [Restful请求]
  22. * 3. form 方式, 支持文件上传, 参数包到 map. body 不允许为空, 导致异常
  23. * 4. response 请求后若需要转为数据类型, 读取请求结果body为JSONString. 若直接获取格式字符串, 不能再转为对象或Map
  24. * -
  25. * - 前端请求格式
  26. * 1. getDefault: url上param, 后端取值@requestParam,也可用request.getParameterMap().getDefault(“key”), 参数会被放入一个集合
  27. * 2. post: body内json, 后端取值@requestBody, Map 或转为实体
  28. * 3. form: body内格式为form, 和content-type有关系, 需要为form格式后端才能读取: 不能使用@RequestBody,参数会自动解析到实体; 若不是实体通过方法转Map
  29. * 4. upload: body-formData, 一般用于文件上传, 追加数据流
  30. */
  31. @Slf4j
  32. public abstract class UtilHttp {
  33. public enum METHOD {
  34. POST,
  35. GET,
  36. PUT,
  37. PATCH,
  38. DELETE,
  39. UPLOAD
  40. }
  41. /******** 创建请求 ********/
  42. // todo: 认证格式 - Authorization:Basic base64(“admin:密码”)
  43. public static String doRequest(METHOD method, String url, Map header, Map<String, Object> param, Object body, Map form, String usr, String pwd) {
  44. log.debug("请求入参, url = {}, header = {}, param = {}, body = {}, form = {}", url, header, param, body, form);
  45. String path = HttpUtil.urlWithForm(url, param, CharsetUtil.CHARSET_UTF_8, true);
  46. HttpRequest request;
  47. switch (method) {
  48. case GET:
  49. request = HttpRequest.get(path);
  50. break;
  51. case PUT:
  52. request = HttpRequest.put(path);
  53. break;
  54. case PATCH:
  55. request = HttpRequest.patch(path);
  56. break;
  57. case DELETE:
  58. request = HttpRequest.delete(path);
  59. break;
  60. default:
  61. request = HttpRequest.post(path);
  62. break;
  63. }
  64. request.addHeaders(header).form(form); // form允许为空
  65. if (ObjectUtil.isNotNull(body)) {
  66. // ppExt: 序列号保留null字段, 不做过滤
  67. request.body(JSON.toJSONString(body, SerializerFeature.WriteMapNullValue));
  68. }
  69. if (StringUtils.isNotBlank(usr) && StringUtils.isNotBlank(pwd)) {
  70. request.basicAuth(usr, pwd);
  71. }
  72. HttpResponse out = request.execute();
  73. log.debug("请求响应, {}, {}", out.getStatus(), out.body()); // http 状态判定
  74. // ppExt: 外部接口http状态异常, 不直接阻断, 通过 r.assertSuccess(); 校验
  75. //McException.assertException(out.getStatus() != 200, String.valueOf(out.getStatus()), "ERROR HTTP STATUS EXCEPTION");
  76. return out.body();
  77. }
  78. public static String doRequest(METHOD method, String url, Map header, Map<String, Object> param, Map body, Map form) {
  79. return doRequest(method, url, header, param, body, form, null, null);
  80. }
  81. public static String doRequest(METHOD method, String url, Map header, Map<String, Object> param, Map body) {
  82. return doRequest(method, url, header, param, body, null);
  83. }
  84. /*** ------------ 创建POST请求 ------------ ***/
  85. public static String doPost(String url, Map header, Map<String, Object> param, Object body, Map form) {
  86. return doRequest(METHOD.POST, url, header, param, body, form, null, null); // 兼容非 map
  87. }
  88. public static String doPost(String url, Map header, Map<String, Object> param, Map body, Map form) {
  89. return doRequest(METHOD.POST, url, header, param, body, form);
  90. }
  91. public static String doPost(String url, Map header, Map<String, Object> param, Map body) {
  92. return doRequest(METHOD.POST, url, header, param, body);
  93. }
  94. public static VenR doPost(String url, Map header, Map<String, Object> param, Map body, Map form, Class rClass) {
  95. String rsp = doPost(url, header, param, body, form);
  96. VenR r = (VenR) JSON.parseObject(rsp, rClass);
  97. r.assertSuccess();
  98. return r;
  99. }
  100. public static VenR doPost(String url, Map header, Map<String, Object> param, Map body, Class rClass) {
  101. return doPost(url, header, param, body, null, rClass);
  102. }
  103. public static VenR doPost(String url, Map header, Map body, Class rClass) {
  104. return doPost(url, header, null, body, rClass);
  105. }
  106. /*** ------------ 创建GET请求 ------------ ***/
  107. public static String doGet(String url, Map header, Map param) {
  108. return doRequest(METHOD.GET, url, header, param, null);
  109. }
  110. public static VenR doGet(String url, Map header, Map param, Class rClass) {
  111. String rsp = doGet(url, header, param);
  112. VenR r = (VenR) JSON.parseObject(rsp, rClass);
  113. // r.assertSuccess();
  114. return r;
  115. }
  116. public static VenR doGet(String url, Map<String, Object> param, Class rClass) {
  117. return doGet(url, null, param, rClass);
  118. }
  119. /*** ------------ 创建PUT请求 ------------ ***/
  120. public static String doPut(String url, Map header, Map param, Map body) {
  121. return doRequest(METHOD.PUT, url, header, param, body);
  122. }
  123. public static VenR doPut(String url, Map header, Map param, Map body, Class rClass) {
  124. String rsp = doPut(url, header, param, body);
  125. VenR r = (VenR) JSON.parseObject(rsp, rClass);
  126. r.assertSuccess();
  127. return r;
  128. }
  129. public static VenR doPut(String url, Map header, Map body, Class rClass) {
  130. return doPut(url, header, null, body, rClass);
  131. }
  132. public static VenR doPut(String url, Map body, Class rClass) {
  133. return doPut(url, null, null, body, rClass);
  134. }
  135. /*** ------------ 创建DELETE请求 ------------ ***/
  136. public static String doDelete(String url, Map header, Map param, Map body) {
  137. return doRequest(METHOD.DELETE, url, header, param, body);
  138. }
  139. public static VenR doDelete(String url, Map header, Map param, Class rClass) {
  140. String rsp = doDelete(url, header, param, (Map) null);
  141. VenR r = (VenR) JSON.parseObject(rsp, rClass);
  142. r.assertSuccess();
  143. return r;
  144. }
  145. /*** ------------ 创建PATCH请求 ------------ ***/
  146. public static String doPatch(String url, Map header, Map param, Map body) {
  147. return doRequest(METHOD.PATCH, url, header, param, body);
  148. }
  149. public static VenR doPatch(String url, Map header, Map param, Map body, Class rClass) {
  150. String rsp = doPatch(url, header, param, body);
  151. VenR r = (VenR) JSON.parseObject(rsp, rClass);
  152. r.assertSuccess();
  153. return r;
  154. }
  155. public static VenR doPatch(String url, Map header, Map body, Class rClass) {
  156. return doPatch(url, header, null, body, rClass);
  157. }
  158. /*** ------------ 文件访问处理 ------------ ***/
  159. public static String doUpload(String url, Map header, Map<String, Object> param, Map form) {
  160. return doPost(url, header, param, null, form);
  161. }
  162. public static VenR doUpload(String url, Map header, Map<String, Object> param, Map form, Class rClass) {
  163. return doPost(url, header, param, null, form, rClass);
  164. }
  165. public static VenR doUpload(String url, Map header, Map form, Class rClass) {
  166. return doPost(url, header, null, null, form, rClass);
  167. }
  168. public static long doDownload(String url, File file) {
  169. return HttpUtil.downloadFile(url, file);
  170. }
  171. /*** ------------ 创建SOAP请求 ------------ ***/
  172. public static Map doSoap(String url, Map param, String methodName, String namespaceURI) {
  173. log.debug("请求入参, url = {}, param = {}, methodName = {}, namespaceURI = {}", url, param, methodName, namespaceURI);
  174. // 新建客户端
  175. SoapClient client = SoapClient.create(url)
  176. // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
  177. .setMethod(methodName, namespaceURI)
  178. // 设置参数,此处自动添加方法的前缀:web2
  179. .setParams(param);
  180. // 发送请求,参数true表示返回一个格式化后的XML内容
  181. // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
  182. String rsp = client.send(true);
  183. log.debug("请求响应, {}", rsp);
  184. return XmlUtil.xmlToMap(rsp);
  185. }
  186. }