|
|
@@ -0,0 +1,180 @@
|
|
|
+package com.malk.Util;
|
|
|
+
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.malk.server.common.VenR;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * HttpUtil [取值详见CatchException]
|
|
|
+ * -
|
|
|
+ * - 后端方法说明
|
|
|
+ * 1. param 使用转码, 避免中文字符导致参数识别异常, 如中文小括号
|
|
|
+ * 2. body 注意 .form 是表单内容; .body 才是请求内容 [Restful请求]
|
|
|
+ * 3. form 方式, 支持文件上传, 参数包到 map. body 不允许为空, 导致异常
|
|
|
+ * 4. response 请求后若需要转为数据类型, 读取请求结果body为JSONString. 若直接获取格式字符串, 不能再转为对象或Map
|
|
|
+ * -
|
|
|
+ * - 前端请求格式
|
|
|
+ * 1. get: url上param, 后端取值@requestParam,也可用request.getParameterMap().get(“key”), 参数会被放入一个集合
|
|
|
+ * 2. post: body内json, 后端取值@requestBody, Map 或转为实体
|
|
|
+ * 3. form: body内格式为form, 和content-type有关系, 需要为form格式后端才能读取: 不能使用@RequestBody,参数会自动解析到实体; 若不是实体通过方法转Map
|
|
|
+ * 4. upload: body-formData, 一般用于文件上传, 追加数据流
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public abstract class UtilHttp {
|
|
|
+
|
|
|
+ /*** ------------ 创建POST请求 ------------ ***/
|
|
|
+
|
|
|
+ public static String doPost(String url, Map header, Map<String, Object> param, Object body, Map form, String usr, String pwd) {
|
|
|
+ log.debug("请求入参, url = {}, header = {}, param = {}, body = {}, form = {}", url, header, param, body, form);
|
|
|
+ String path = HttpUtil.urlWithForm(url, param, CharsetUtil.CHARSET_UTF_8, true);
|
|
|
+ HttpRequest request = HttpRequest.post(path).addHeaders(header).form(form); // 允许为空
|
|
|
+ if (ObjectUtil.isNotNull(body)) {
|
|
|
+ request.body(JSON.toJSONString(body));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(usr) && StringUtils.isNotBlank(pwd)) {
|
|
|
+ request.basicAuth(usr, pwd);
|
|
|
+ }
|
|
|
+ String rsp = request.execute().body();
|
|
|
+ log.debug("请求响应, {}", rsp);
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doPost(String url, Map header, Map<String, Object> param, Map body, Map form) {
|
|
|
+ return doPost(url, header, param, body, form, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doPost(String url, Map header, Map<String, Object> param, Map body) {
|
|
|
+ return doPost(url, header, param, body, null, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String doUpload(String url, Map header, Map<String, Object> param, Map form) {
|
|
|
+ return doPost(url, header, param, null, form, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPost(String url, Map header, Map<String, Object> param, Map body, Map form, Class rClass) {
|
|
|
+ String rsp = doPost(url, header, param, body, form);
|
|
|
+ VenR r = (VenR) JSON.parseObject(rsp, rClass);
|
|
|
+ r.assertSuccess();
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPost(String url, Map header, Map<String, Object> param, Map body, Class rClass) {
|
|
|
+ return doPost(url, header, param, body, null, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPost(String url, Map header, Map body, Class rClass) {
|
|
|
+ return doPost(url, header, null, body, null, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doUpload(String url, Map header, Map<String, Object> param, Map form, Class rClass) {
|
|
|
+ return doPost(url, header, param, null, form, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doUpload(String url, Map header, Map form, Class rClass) {
|
|
|
+ return doUpload(url, header, null, form, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*** ------------ 创建GET请求 ------------ ***/
|
|
|
+
|
|
|
+ public static String doGet(String url, Map header, Map param) {
|
|
|
+ log.debug("请求入参, {}, {}, {}", url, header, param);
|
|
|
+ String path = HttpUtil.urlWithForm(url, param, CharsetUtil.CHARSET_UTF_8, true);
|
|
|
+ String rsp = HttpRequest.get(path).addHeaders(header).execute().body();
|
|
|
+ log.debug("请求响应, {}", rsp);
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doGet(String url, Map header, Map param, Class rClass) {
|
|
|
+ String rsp = doGet(url, header, param);
|
|
|
+ VenR r = (VenR) JSON.parseObject(rsp, rClass);
|
|
|
+ r.assertSuccess();
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doGet(String url, Map<String, Object> param, Class rClass) {
|
|
|
+ return doGet(url, null, param, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*** ------------ 创建PUT请求 ------------ ***/
|
|
|
+
|
|
|
+ public static String doPut(String url, Map header, Map param, Map body) {
|
|
|
+ log.debug("请求入参, {}, {}, {}, {}", url, header, param, body);
|
|
|
+ String path = HttpUtil.urlWithForm(url, param, CharsetUtil.CHARSET_UTF_8, true);
|
|
|
+ HttpRequest request = HttpRequest.put(path).addHeaders(header);
|
|
|
+ if (ObjectUtil.isNotNull(body)) {
|
|
|
+ request.body(JSON.toJSONString(body));
|
|
|
+ }
|
|
|
+ String rsp = request.execute().body();
|
|
|
+ log.debug("请求响应, {}", rsp);
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPut(String url, Map header, Map param, Map body, Class rClass) {
|
|
|
+ String rsp = doPut(url, header, param, body);
|
|
|
+ VenR r = (VenR) JSON.parseObject(rsp, rClass);
|
|
|
+ r.assertSuccess();
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPut(String url, Map header, Map body, Class rClass) {
|
|
|
+ return doPut(url, header, null, body, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPut(String url, Map body, Class rClass) {
|
|
|
+ return doPut(url, null, null, body, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*** ------------ 创建DELETE请求 ------------ ***/
|
|
|
+
|
|
|
+ public static String doDelete(String url, Map header) {
|
|
|
+ log.debug("请求入参, {}, {}, {}", url, header);
|
|
|
+ String rsp = HttpRequest.delete(url).addHeaders(header).execute().body();
|
|
|
+ log.debug("请求响应, {}", rsp);
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doDelete(String url, Map header, Class rClass) {
|
|
|
+ String rsp = doDelete(url, header);
|
|
|
+ VenR r = (VenR) JSON.parseObject(rsp, rClass);
|
|
|
+ r.assertSuccess();
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*** ------------ 创建PATCH请求 ------------ ***/
|
|
|
+
|
|
|
+ public static String doPatch(String url, Map header, Map param, Map body) {
|
|
|
+ log.debug("请求入参, {}, {}, {}, {}", url, header, param, body);
|
|
|
+ String path = HttpUtil.urlWithForm(url, param, CharsetUtil.CHARSET_UTF_8, true);
|
|
|
+ HttpRequest request = HttpRequest.patch(path).addHeaders(header);
|
|
|
+ if (ObjectUtil.isNotNull(body)) {
|
|
|
+ request.body(JSON.toJSONString(body));
|
|
|
+ }
|
|
|
+ String rsp = request.execute().body();
|
|
|
+ log.debug("请求响应, {}", rsp);
|
|
|
+ return rsp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPatch(String url, Map header, Map param, Map body, Class rClass) {
|
|
|
+ String rsp = doPatch(url, header, param, body);
|
|
|
+ VenR r = (VenR) JSON.parseObject(rsp, rClass);
|
|
|
+ r.assertSuccess();
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPatch(String url, Map header, Map body, Class rClass) {
|
|
|
+ return doPatch(url, header, null, body, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static VenR doPatch(String url, Map body, Class rClass) {
|
|
|
+ return doPatch(url, null, null, body, rClass);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|