From a2a0b218f698365eceb1cc25e531b380d5fbe87a Mon Sep 17 00:00:00 2001 From: yanrongzhen Date: Fri, 14 Oct 2022 17:32:55 +0800 Subject: [PATCH] fix post bug --- .../common/toolkit/http/HttpUtils.java | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtils.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtils.java index 6c42dca3..acc946d0 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtils.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/http/HttpUtils.java @@ -31,8 +31,11 @@ import lombok.extern.slf4j.Slf4j; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLEncoder; import java.util.Map; +import static cn.hippo4j.common.constant.HttpHeaderConsts.CONTENT_LENGTH; + /** * Http request utilities. * @author Rongzhen Yan @@ -47,39 +50,44 @@ public class HttpUtils { private HttpUtils() { } - public static T post(String url, Object param, Class clazz) { - String result = post(url, param); + public static T post(String url, Object body, Class clazz) { + String result = post(url, body); + return JSONUtil.parseObject(result, clazz); + } + + public static T post(String url, Object body, long timeoutMillis, Class clazz) { + String result = post(url, body, timeoutMillis); return JSONUtil.parseObject(result, clazz); } - public static T post(String url, Object param, long timeoutMillis, Class clazz) { - String result = post(url, param, timeoutMillis); + public static T post(String url, Map headers, Map params, long timeoutMillis, Class clazz) { + String result = execute(buildUrl(url, params), HttpMethod.POST, null, headers, timeoutMillis).getBodyString(); return JSONUtil.parseObject(result, clazz); } - public static T post(String url, Map headers, Object param, long timeoutMillis, Class clazz) { - String result = execute(url, HttpMethod.POST, param, headers, timeoutMillis).getBodyString(); + public static T post(String url, Map headers, Object body, long timeoutMillis, Class clazz) { + String result = execute(url, HttpMethod.POST, body, headers, timeoutMillis).getBodyString(); return JSONUtil.parseObject(result, clazz); } - public static String post(String url, Object param) { - return execute(url, HttpMethod.POST, param, null).getBodyString(); + public static String post(String url, Object body) { + return execute(url, HttpMethod.POST, body, null).getBodyString(); } - public static String post(String url, Object param, long timeoutMillis) { - return execute(url, HttpMethod.POST, param, null, timeoutMillis).getBodyString(); + public static String post(String url, Object body, long timeoutMillis) { + return execute(url, HttpMethod.POST, body, null, timeoutMillis).getBodyString(); } public static String postJson(String url, String json) { return executeJson(url, HttpMethod.POST, json, null).getBodyString(); } - public static String put(String url, Object param) { - return execute(url, HttpMethod.PUT, param, null).getBodyString(); + public static String put(String url, Object body) { + return execute(url, HttpMethod.PUT, body, null).getBodyString(); } - public static String put(String url, Object param, Map headers) { - return execute(url, HttpMethod.PUT, param, headers).getBodyString(); + public static String put(String url, Object body, Map headers) { + return execute(url, HttpMethod.PUT, body, headers).getBodyString(); } public static T get(String url, Map headers, Map params, long readTimeoutMillis, Class clazz) { @@ -134,7 +142,7 @@ public class HttpUtils { } @SneakyThrows - public static HttpClientResponse doExecute(HttpURLConnection connection, Object param, Map headers) { + public static HttpClientResponse doExecute(HttpURLConnection connection, Object body, Map headers) { try { if (headers != null) { for (String key : headers.keySet()) { @@ -142,14 +150,17 @@ public class HttpUtils { } } String bodyString; - if (param instanceof String) { - bodyString = (String) param; + if (body instanceof String) { + bodyString = (String) body; } else { - bodyString = JSONUtil.toJSONString(param); + bodyString = JSONUtil.toJSONString(body); } - if (StringUtil.isNotEmpty(bodyString)) { + if (!StringUtil.isEmpty(bodyString)) { + connection.setDoOutput(true); + byte[] b = bodyString.getBytes(); + connection.setRequestProperty(CONTENT_LENGTH, String.valueOf(b.length)); OutputStream outputStream = connection.getOutputStream(); - outputStream.write(bodyString.getBytes(Constants.ENCODE)); + outputStream.write(b, 0, b.length); outputStream.flush(); IoUtils.closeQuietly(outputStream); } @@ -164,15 +175,15 @@ public class HttpUtils { log.error(LogMessage.getInstance().setMsg("Http Call error.") .kv("url", connection.getURL()) .kv("method", connection.getRequestMethod()) - .kv("param", JSONUtil.toJSONString(param)) + .kv("body", JSONUtil.toJSONString(body)) .kv2String("headers", JSONUtil.toJSONString(headers)), e); throw e; } } - public static HttpClientResponse execute(String url, String method, Object param, Map headers, long timeout) { + public static HttpClientResponse execute(String url, String method, Object body, Map headers, long timeout) { HttpURLConnection connection = createConnection(url, method, timeout); - return doExecute(connection, param, headers); + return doExecute(connection, body, headers); } public static HttpClientResponse executeJson(String url, String method, String json, Map headers) { @@ -201,7 +212,7 @@ public class HttpUtils { @SneakyThrows private static HttpURLConnection createConnection(String url, String method, long timeout) { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL(URLEncoder.encode(url)).openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(Integer.parseInt(String.valueOf(timeout)));