|
|
@@ -20,6 +20,7 @@ import java.net.URI;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
@Slf4j
|
|
|
public class HttpClientUtils {
|
|
|
@@ -111,7 +112,9 @@ public class HttpClientUtils {
|
|
|
}
|
|
|
|
|
|
public static String doPostJson(String url, String json) {
|
|
|
- log.info(">>> 请求URL={}, 请求体={}", url, json);
|
|
|
+ final String requestId = UUID.randomUUID().toString();
|
|
|
+ final long start = System.currentTimeMillis();
|
|
|
+ log.info("[{}] >>> POST JSON 请求开始 URL={}, body={}", requestId, url, json);
|
|
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
String resultString = "";
|
|
|
@@ -128,19 +131,44 @@ public class HttpClientUtils {
|
|
|
httpPost.setEntity(entity);
|
|
|
// 执行http请求
|
|
|
response = HTTP_CLIENT.execute(httpPost);
|
|
|
- resultString = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ int statusCode = response.getStatusLine() != null ? response.getStatusLine().getStatusCode() : -1;
|
|
|
+ resultString = response.getEntity() == null ? "" : EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+
|
|
|
+ long costMs = System.currentTimeMillis() - start;
|
|
|
+ String preview = resultString == null ? "null" : (resultString.length() > 500 ? resultString.substring(0, 500) + "...(truncated)" : resultString);
|
|
|
+
|
|
|
+ if (statusCode != 200) {
|
|
|
+ log.warn("[{}] <<< POST JSON 响应异常 status={} costMs={} preview={}", requestId, statusCode, costMs, preview);
|
|
|
+ } else if (resultString == null || resultString.trim().isEmpty()) {
|
|
|
+ log.warn("[{}] <<< POST JSON 响应为空 status=200 costMs={}", requestId, costMs);
|
|
|
+ } else {
|
|
|
+ log.info("[{}] <<< POST JSON 响应成功 status=200 costMs={} preview={}", requestId, costMs, preview);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ long costMs = System.currentTimeMillis() - start;
|
|
|
+ String root = buildRootCause(e);
|
|
|
+ log.error("[{}] !!! POST JSON 请求异常 costMs={} URL={} body={} exType={} exMsg={} rootCause={}",
|
|
|
+ requestId, costMs, url, json, e.getClass().getName(), e.getMessage(), root, e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (response != null) {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.warn("[{}] 关闭HTTP响应失败: {}", requestId, e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return resultString;
|
|
|
}
|
|
|
+
|
|
|
+ private static String buildRootCause(Throwable t) {
|
|
|
+ if (t == null) return "null";
|
|
|
+ Throwable cur = t;
|
|
|
+ while (cur.getCause() != null && cur.getCause() != cur) {
|
|
|
+ cur = cur.getCause();
|
|
|
+ }
|
|
|
+ String msg = cur.getMessage();
|
|
|
+ return cur.getClass().getName() + (msg == null ? "" : (": " + msg));
|
|
|
+ }
|
|
|
}
|