|
|
@@ -81,9 +81,9 @@ public class HttpUtils {
|
|
|
return sendGet(url, toQueryString(param));
|
|
|
}
|
|
|
|
|
|
- public static String sendGet(String url, Object param,HashMap<String, String> headers) {
|
|
|
+ public static String sendGet(String url, Object param, HashMap<String, String> headers) throws IOException {
|
|
|
|
|
|
- return sendGet(url, toQueryString(param),headers);
|
|
|
+ return sendGet(url, toQueryString(param), headers);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -133,8 +133,9 @@ public class HttpUtils {
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
- public static String sendGet(String url, String param, HashMap<String, String> headers) {
|
|
|
+ public static String sendGet(String url, String param, HashMap<String, String> headers) throws IOException {
|
|
|
|
|
|
+ HashMap<String, String> hashMap = new HashMap<>();
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
BufferedReader in = null;
|
|
|
try {
|
|
|
@@ -152,19 +153,12 @@ public class HttpUtils {
|
|
|
|
|
|
connection.connect();
|
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), Constants.UTF8));
|
|
|
+
|
|
|
String line;
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
result.append(line);
|
|
|
}
|
|
|
log.info("recv - {}", result);
|
|
|
- } catch (ConnectException e) {
|
|
|
- log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
- } catch (SocketTimeoutException e) {
|
|
|
- log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (in != null) {
|
|
|
@@ -177,6 +171,52 @@ public class HttpUtils {
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public static HashMap<String, String> sendHttpGet(String url, String param, HashMap<String, String> headers) throws IOException {
|
|
|
+ String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
|
|
|
+
|
|
|
+ HashMap<String, String> hashMap = new HashMap<>();
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+
|
|
|
+
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(urlNameString);
|
|
|
+ conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+
|
|
|
+
|
|
|
+ // 设置通用请求头
|
|
|
+ conn.setRequestProperty("Accept", "application/json");
|
|
|
+ if (StringUtils.isNotEmpty(headers)) {
|
|
|
+ //headers.remove("Content-Type"); // 防御性移除外部可能传入的冲突头
|
|
|
+ headers.forEach(conn::setRequestProperty);
|
|
|
+ }
|
|
|
+ conn.setDoOutput(false);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setConnectTimeout(5000); // 设置超时
|
|
|
+ conn.setReadTimeout(10000);
|
|
|
+
|
|
|
+// 检查 HTTP 状态码
|
|
|
+ int status = conn.getResponseCode();
|
|
|
+ hashMap.put("status", status + "");
|
|
|
+ // 读取响应(显式 UTF-8 解码)
|
|
|
+ try (BufferedReader in = new BufferedReader(
|
|
|
+ new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ hashMap.put("result", result.toString());
|
|
|
+ return hashMap;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
|
@@ -284,16 +324,16 @@ public class HttpUtils {
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
- public static String sendBodyPost(String url, Object body, HashMap<String, String> headers) throws IOException {
|
|
|
+ public static HashMap<String, String> sendBodyPost(String url, Object body, HashMap<String, String> headers) throws IOException {
|
|
|
String bodyJson = JsonUtils.objectToJson(body);
|
|
|
- System.out.println(bodyJson);
|
|
|
- return sendBodyPostTest( url, bodyJson, headers);
|
|
|
+ return sendBodyPostTest(url, bodyJson, headers);
|
|
|
}
|
|
|
|
|
|
- public static String sendBodyPostTest(String url, String param, HashMap<String, String> headers) throws IOException {
|
|
|
- if (url == null || param == null) {
|
|
|
- throw new IllegalArgumentException("URL and parameters cannot be null");
|
|
|
+ public static HashMap<String, String> sendBodyPostTest(String url, String param, HashMap<String, String> headers) throws IOException {
|
|
|
+ if (url == null) {
|
|
|
+ throw new IllegalArgumentException("网址为空");
|
|
|
}
|
|
|
+ HashMap<String, String> hashMap = new HashMap<>();
|
|
|
|
|
|
HttpURLConnection conn = null;
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
@@ -325,10 +365,7 @@ public class HttpUtils {
|
|
|
|
|
|
// 检查 HTTP 状态码
|
|
|
int status = conn.getResponseCode();
|
|
|
- if (status != HttpURLConnection.HTTP_OK) {
|
|
|
- throw new IOException("HTTP error code: " + status);
|
|
|
- }
|
|
|
-
|
|
|
+ hashMap.put("status", status + "");
|
|
|
// 读取响应(显式 UTF-8 解码)
|
|
|
try (BufferedReader in = new BufferedReader(
|
|
|
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
@@ -339,18 +376,13 @@ public class HttpUtils {
|
|
|
}
|
|
|
|
|
|
log.info("Request succeeded, response: {}", result);
|
|
|
- } catch (ConnectException | SocketTimeoutException e) {
|
|
|
- log.error("Connection error: {}", url, e);
|
|
|
- throw e;
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("I/O error: {}", url, e);
|
|
|
- throw e;
|
|
|
} finally {
|
|
|
if (conn != null) {
|
|
|
conn.disconnect();
|
|
|
}
|
|
|
}
|
|
|
- return result.toString();
|
|
|
+ hashMap.put("result", result.toString());
|
|
|
+ return hashMap;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -584,7 +616,8 @@ public class HttpUtils {
|
|
|
|
|
|
return URLEncodedUtils.format(params, StandardCharsets.UTF_8);
|
|
|
}
|
|
|
- public static void setGatewayHeaders(HashMap<String, String> hashMap){
|
|
|
+
|
|
|
+ public static void setGatewayHeaders(HashMap<String, String> hashMap) {
|
|
|
hashMap.put("Authorization", "1478702065085063169");
|
|
|
}
|
|
|
}
|