fix post bug

pull/800/head
yanrongzhen 3 years ago
parent e045746602
commit a2a0b218f6

@ -31,8 +31,11 @@ import lombok.extern.slf4j.Slf4j;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.util.Map; import java.util.Map;
import static cn.hippo4j.common.constant.HttpHeaderConsts.CONTENT_LENGTH;
/** /**
* Http request utilities. * Http request utilities.
* @author Rongzhen Yan * @author Rongzhen Yan
@ -47,39 +50,44 @@ public class HttpUtils {
private HttpUtils() { private HttpUtils() {
} }
public static <T> T post(String url, Object param, Class<T> clazz) { public static <T> T post(String url, Object body, Class<T> clazz) {
String result = post(url, param); String result = post(url, body);
return JSONUtil.parseObject(result, clazz);
}
public static <T> T post(String url, Object body, long timeoutMillis, Class<T> clazz) {
String result = post(url, body, timeoutMillis);
return JSONUtil.parseObject(result, clazz); return JSONUtil.parseObject(result, clazz);
} }
public static <T> T post(String url, Object param, long timeoutMillis, Class<T> clazz) { public static <T> T post(String url, Map<String, String> headers, Map<String, String> params, long timeoutMillis, Class<T> clazz) {
String result = post(url, param, timeoutMillis); String result = execute(buildUrl(url, params), HttpMethod.POST, null, headers, timeoutMillis).getBodyString();
return JSONUtil.parseObject(result, clazz); return JSONUtil.parseObject(result, clazz);
} }
public static <T> T post(String url, Map<String, String> headers, Object param, long timeoutMillis, Class<T> clazz) { public static <T> T post(String url, Map<String, String> headers, Object body, long timeoutMillis, Class<T> clazz) {
String result = execute(url, HttpMethod.POST, param, headers, timeoutMillis).getBodyString(); String result = execute(url, HttpMethod.POST, body, headers, timeoutMillis).getBodyString();
return JSONUtil.parseObject(result, clazz); return JSONUtil.parseObject(result, clazz);
} }
public static String post(String url, Object param) { public static String post(String url, Object body) {
return execute(url, HttpMethod.POST, param, null).getBodyString(); return execute(url, HttpMethod.POST, body, null).getBodyString();
} }
public static String post(String url, Object param, long timeoutMillis) { public static String post(String url, Object body, long timeoutMillis) {
return execute(url, HttpMethod.POST, param, null, timeoutMillis).getBodyString(); return execute(url, HttpMethod.POST, body, null, timeoutMillis).getBodyString();
} }
public static String postJson(String url, String json) { public static String postJson(String url, String json) {
return executeJson(url, HttpMethod.POST, json, null).getBodyString(); return executeJson(url, HttpMethod.POST, json, null).getBodyString();
} }
public static String put(String url, Object param) { public static String put(String url, Object body) {
return execute(url, HttpMethod.PUT, param, null).getBodyString(); return execute(url, HttpMethod.PUT, body, null).getBodyString();
} }
public static String put(String url, Object param, Map<String, String> headers) { public static String put(String url, Object body, Map<String, String> headers) {
return execute(url, HttpMethod.PUT, param, headers).getBodyString(); return execute(url, HttpMethod.PUT, body, headers).getBodyString();
} }
public static <T> T get(String url, Map<String, String> headers, Map<String, String> params, long readTimeoutMillis, Class<T> clazz) { public static <T> T get(String url, Map<String, String> headers, Map<String, String> params, long readTimeoutMillis, Class<T> clazz) {
@ -134,7 +142,7 @@ public class HttpUtils {
} }
@SneakyThrows @SneakyThrows
public static HttpClientResponse doExecute(HttpURLConnection connection, Object param, Map<String, String> headers) { public static HttpClientResponse doExecute(HttpURLConnection connection, Object body, Map<String, String> headers) {
try { try {
if (headers != null) { if (headers != null) {
for (String key : headers.keySet()) { for (String key : headers.keySet()) {
@ -142,14 +150,17 @@ public class HttpUtils {
} }
} }
String bodyString; String bodyString;
if (param instanceof String) { if (body instanceof String) {
bodyString = (String) param; bodyString = (String) body;
} else { } 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 outputStream = connection.getOutputStream();
outputStream.write(bodyString.getBytes(Constants.ENCODE)); outputStream.write(b, 0, b.length);
outputStream.flush(); outputStream.flush();
IoUtils.closeQuietly(outputStream); IoUtils.closeQuietly(outputStream);
} }
@ -164,15 +175,15 @@ public class HttpUtils {
log.error(LogMessage.getInstance().setMsg("Http Call error.") log.error(LogMessage.getInstance().setMsg("Http Call error.")
.kv("url", connection.getURL()) .kv("url", connection.getURL())
.kv("method", connection.getRequestMethod()) .kv("method", connection.getRequestMethod())
.kv("param", JSONUtil.toJSONString(param)) .kv("body", JSONUtil.toJSONString(body))
.kv2String("headers", JSONUtil.toJSONString(headers)), e); .kv2String("headers", JSONUtil.toJSONString(headers)), e);
throw e; throw e;
} }
} }
public static HttpClientResponse execute(String url, String method, Object param, Map<String, String> headers, long timeout) { public static HttpClientResponse execute(String url, String method, Object body, Map<String, String> headers, long timeout) {
HttpURLConnection connection = createConnection(url, method, 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<String, String> headers) { public static HttpClientResponse executeJson(String url, String method, String json, Map<String, String> headers) {
@ -201,7 +212,7 @@ public class HttpUtils {
@SneakyThrows @SneakyThrows
private static HttpURLConnection createConnection(String url, String method, long timeout) { 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.setDoInput(true);
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setConnectTimeout(Integer.parseInt(String.valueOf(timeout))); connection.setConnectTimeout(Integer.parseInt(String.valueOf(timeout)));

Loading…
Cancel
Save