|
|
|
@ -21,106 +21,224 @@ import cn.hippo4j.common.constant.Constants;
|
|
|
|
|
import cn.hippo4j.common.constant.HttpMediaType;
|
|
|
|
|
import cn.hippo4j.common.constant.HttpMethod;
|
|
|
|
|
import cn.hippo4j.common.constant.HttpResponseCode;
|
|
|
|
|
import cn.hippo4j.common.toolkit.IoUtils;
|
|
|
|
|
import cn.hippo4j.common.toolkit.CollectionUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.IoUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.StringUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.logtracing.LogMessage;
|
|
|
|
|
import cn.hippo4j.common.web.exception.ServiceException;
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
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 java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import static cn.hippo4j.common.constant.HttpHeaderConsts.CONTENT_LENGTH;
|
|
|
|
|
import static cn.hippo4j.common.constant.HttpHeaderConstants.CONTENT_LENGTH;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Http request utilities.
|
|
|
|
|
*
|
|
|
|
|
* @author Rongzhen Yan
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class HttpUtils {
|
|
|
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
|
|
|
|
public class HttpUtil {
|
|
|
|
|
|
|
|
|
|
private static final int CONNECT_TIMEOUT = 10000;
|
|
|
|
|
/**
|
|
|
|
|
* Default connect timeout
|
|
|
|
|
*/
|
|
|
|
|
private static final int DEFAULT_CONNECT_TIMEOUT = 10000;
|
|
|
|
|
|
|
|
|
|
private static final int READ_TIMEOUT = 300000;
|
|
|
|
|
/**
|
|
|
|
|
* Default read timeout
|
|
|
|
|
*/
|
|
|
|
|
private static final int DEFAULT_READ_TIMEOUT = 30000;
|
|
|
|
|
|
|
|
|
|
private HttpUtils() {
|
|
|
|
|
/**
|
|
|
|
|
* Send a get network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param headers headers
|
|
|
|
|
* @param params form data
|
|
|
|
|
* @param timeout request timeout
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T get(String url, Map<String, String> headers, Map<String, String> params, long timeout, Class<T> clazz) {
|
|
|
|
|
return execute(buildUrl(url, params), HttpMethod.GET, null, headers, timeout, clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a get network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param params form data
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String get(String url, Map<String, String> params) {
|
|
|
|
|
return execute(buildUrl(url, params), HttpMethod.GET, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a get network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String get(String url) {
|
|
|
|
|
return execute(url, HttpMethod.GET, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a get network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T get(String url, Class<T> clazz) {
|
|
|
|
|
return JSONUtil.parseObject(get(url), clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T post(String url, Object body, Class<T> clazz) {
|
|
|
|
|
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);
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @param timeout request timeout
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T post(String url, Object body, long timeout, Class<T> clazz) {
|
|
|
|
|
String result = post(url, body, timeout);
|
|
|
|
|
return JSONUtil.parseObject(result, clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <T> T post(String url, Map<String, String> headers, Map<String, String> params, long timeoutMillis, Class<T> clazz) {
|
|
|
|
|
String result = execute(buildUrl(url, params), HttpMethod.POST, null, headers, timeoutMillis).getBodyString();
|
|
|
|
|
return JSONUtil.parseObject(result, clazz);
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param headers headers
|
|
|
|
|
* @param params form data
|
|
|
|
|
* @param timeout request timeout
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T post(String url, Map<String, String> headers, Map<String, String> params, long timeout, Class<T> clazz) {
|
|
|
|
|
return execute(buildUrl(url, params), HttpMethod.POST, null, headers, timeout, 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, body, headers, timeoutMillis).getBodyString();
|
|
|
|
|
return JSONUtil.parseObject(result, clazz);
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param headers headers
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @param timeout request timeout
|
|
|
|
|
* @param clazz return the target data type
|
|
|
|
|
* @param <T> return the target data type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T post(String url, Map<String, String> headers, Object body, long timeout, Class<T> clazz) {
|
|
|
|
|
return execute(url, HttpMethod.POST, body, headers, timeout, clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String post(String url, Object body) {
|
|
|
|
|
return execute(url, HttpMethod.POST, body, null).getBodyString();
|
|
|
|
|
return execute(url, HttpMethod.POST, body, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String post(String url, Object body, long timeoutMillis) {
|
|
|
|
|
return execute(url, HttpMethod.POST, body, null, timeoutMillis).getBodyString();
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @param timeout request timeout
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String post(String url, Object body, long timeout) {
|
|
|
|
|
return execute(url, HttpMethod.POST, body, null, timeout, String.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a post network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param json json data
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String postJson(String url, String json) {
|
|
|
|
|
return executeJson(url, HttpMethod.POST, json, null).getBodyString();
|
|
|
|
|
return executeJson(url, HttpMethod.POST, json, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a put network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String put(String url, Object body) {
|
|
|
|
|
return execute(url, HttpMethod.PUT, body, null).getBodyString();
|
|
|
|
|
return execute(url, HttpMethod.PUT, body, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a put network request.
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param body request body
|
|
|
|
|
* @param headers headers
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String put(String url, Object body, Map<String, String> headers) {
|
|
|
|
|
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) {
|
|
|
|
|
String result = execute(buildUrl(url, params), HttpMethod.GET, null, headers, readTimeoutMillis).getBodyString();
|
|
|
|
|
return JSONUtil.parseObject(result, clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String get(String url, Map<String, String> params) {
|
|
|
|
|
return execute(buildUrl(url, params), HttpMethod.GET, null, null).getBodyString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String get(String url) {
|
|
|
|
|
return execute(url, HttpMethod.GET, null, null).getBodyString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <T> T get(String url, Class<T> clazz) {
|
|
|
|
|
return JSONUtil.parseObject(get(url), clazz);
|
|
|
|
|
return execute(url, HttpMethod.PUT, body, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a complete Url from the query string.
|
|
|
|
|
* @param url
|
|
|
|
|
* @param queryString
|
|
|
|
|
*
|
|
|
|
|
* @param url target url
|
|
|
|
|
* @param queryParams query params
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
public static String buildUrl(String url, Map<String, String> queryString) {
|
|
|
|
|
if (null == queryString) {
|
|
|
|
|
public static String buildUrl(String url, Map<String, String> queryParams) {
|
|
|
|
|
if (CollectionUtil.isEmpty(queryParams)) {
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
StringBuilder builder = new StringBuilder(url);
|
|
|
|
|
boolean isFirst = true;
|
|
|
|
|
for (Map.Entry<String, String> entry : queryString.entrySet()) {
|
|
|
|
|
StringBuilder builder = new StringBuilder(url);
|
|
|
|
|
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
if (key != null && entry.getValue() != null) {
|
|
|
|
|
if (isFirst) {
|
|
|
|
@ -129,7 +247,7 @@ public class HttpUtils {
|
|
|
|
|
} else {
|
|
|
|
|
builder.append("&");
|
|
|
|
|
}
|
|
|
|
|
String value = URLEncoder.encode(queryString.get(key), Constants.ENCODE)
|
|
|
|
|
String value = URLEncoder.encode(queryParams.get(key), Constants.ENCODE)
|
|
|
|
|
.replaceAll("\\+", "%20");
|
|
|
|
|
builder.append(key)
|
|
|
|
|
.append("=")
|
|
|
|
@ -139,17 +257,49 @@ public class HttpUtils {
|
|
|
|
|
return builder.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HttpClientResponse execute(String url, String method, Object param, Map<String, String> headers) {
|
|
|
|
|
private static String executeJson(String url, String method, String json, Map<String, String> headers) {
|
|
|
|
|
if (!JSONUtil.isJson(json)) {
|
|
|
|
|
log.error(LogMessage.getInstance().setMsg("Http Call error.")
|
|
|
|
|
.kv("url", url)
|
|
|
|
|
.kv("method", method)
|
|
|
|
|
.kv("json", json)
|
|
|
|
|
.kv2String("headers", JSONUtil.toJSONString(headers)));
|
|
|
|
|
throw new ServiceException("Invalid http json body, please check it again.");
|
|
|
|
|
}
|
|
|
|
|
return execute(url, method, json, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String execute(String url, String method, Object param, Map<String, String> headers) {
|
|
|
|
|
HttpURLConnection connection = createConnection(url, method);
|
|
|
|
|
return doExecute(connection, param, headers);
|
|
|
|
|
HttpClientResponse response = null;
|
|
|
|
|
try {
|
|
|
|
|
response = doExecute(connection, param, headers);
|
|
|
|
|
return response.getBodyString();
|
|
|
|
|
} finally {
|
|
|
|
|
Optional.ofNullable(response).ifPresent(each -> each.close());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T> T execute(String url, String method, Object body, Map<String, String> headers, long timeout, Class<T> clazz) {
|
|
|
|
|
HttpURLConnection connection = createConnection(url, method, timeout);
|
|
|
|
|
HttpClientResponse response = null;
|
|
|
|
|
try {
|
|
|
|
|
response = doExecute(connection, body, headers);
|
|
|
|
|
if (clazz == String.class) {
|
|
|
|
|
return (T) response.getBodyString();
|
|
|
|
|
}
|
|
|
|
|
return JSONUtil.parseObject(response.getBodyString(), clazz);
|
|
|
|
|
} finally {
|
|
|
|
|
Optional.ofNullable(response).ifPresent(each -> each.close());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
public static HttpClientResponse doExecute(HttpURLConnection connection, Object body, Map<String, String> headers) {
|
|
|
|
|
private static HttpClientResponse doExecute(HttpURLConnection connection, Object body, Map<String, String> headers) {
|
|
|
|
|
try {
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
for (String key : headers.keySet()) {
|
|
|
|
|
connection.setRequestProperty(key, headers.get(key));
|
|
|
|
|
if (headers != null && headers.size() > 0) {
|
|
|
|
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
|
|
connection.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String bodyString;
|
|
|
|
@ -165,7 +315,7 @@ public class HttpUtils {
|
|
|
|
|
OutputStream outputStream = connection.getOutputStream();
|
|
|
|
|
outputStream.write(b, 0, b.length);
|
|
|
|
|
outputStream.flush();
|
|
|
|
|
IoUtils.closeQuietly(outputStream);
|
|
|
|
|
IoUtil.closeQuietly(outputStream);
|
|
|
|
|
}
|
|
|
|
|
connection.connect();
|
|
|
|
|
JdkHttpClientResponse response = new JdkHttpClientResponse(connection);
|
|
|
|
@ -174,31 +324,14 @@ public class HttpUtils {
|
|
|
|
|
throw new ServiceException(msg);
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
log.error(LogMessage.getInstance().setMsg("Http Call error.")
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
log.error(LogMessage.getInstance().setMsg("Http call error. ")
|
|
|
|
|
.kv("url", connection.getURL())
|
|
|
|
|
.kv("method", connection.getRequestMethod())
|
|
|
|
|
.kv("body", JSONUtil.toJSONString(body))
|
|
|
|
|
.kv2String("headers", JSONUtil.toJSONString(headers)), e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HttpClientResponse execute(String url, String method, Object body, Map<String, String> headers, long timeout) {
|
|
|
|
|
HttpURLConnection connection = createConnection(url, method, timeout);
|
|
|
|
|
return doExecute(connection, body, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HttpClientResponse executeJson(String url, String method, String json, Map<String, String> headers) {
|
|
|
|
|
if (!JSONUtil.isJson(json)) {
|
|
|
|
|
log.error(LogMessage.getInstance().setMsg("Http Call error.")
|
|
|
|
|
.kv("url", url)
|
|
|
|
|
.kv("method", method)
|
|
|
|
|
.kv("json", json)
|
|
|
|
|
.kv2String("headers", JSONUtil.toJSONString(headers)));
|
|
|
|
|
throw new ServiceException("invalid http json body, please check it again.");
|
|
|
|
|
.kv2String("headers", JSONUtil.toJSONString(headers)), ex);
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
return execute(url, method, json, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
@ -206,8 +339,8 @@ public class HttpUtils {
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setConnectTimeout(CONNECT_TIMEOUT);
|
|
|
|
|
connection.setReadTimeout(READ_TIMEOUT);
|
|
|
|
|
connection.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
|
|
|
|
|
connection.setReadTimeout(DEFAULT_READ_TIMEOUT);
|
|
|
|
|
connection.setRequestMethod(method);
|
|
|
|
|
connection.setRequestProperty(Constants.CONTENT_TYPE, HttpMediaType.APPLICATION_JSON);
|
|
|
|
|
return connection;
|
|
|
|
@ -216,7 +349,6 @@ public class HttpUtils {
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
private static HttpURLConnection createConnection(String url, String method, long timeout) {
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
|
|
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setConnectTimeout(Integer.parseInt(String.valueOf(timeout)));
|