private static String getRemoteCallLogStr(HttpUriRequest httpUriRequest, HttpResponse httpResponse, long start, String method, Exception ex, String siteName) {
String startTime = DateFormatUtils.format(new Date(start), "yyyy-MM-dd'T'HH:mm:ss.SSS");
String path = "";
String targetUrl = "";
String uri = "";
String targetHost = "";
String requestheader = "";
String responseHeader = "";
String responseBody = "";
Integer responseStatus = null;
if (!Objects.isNull(httpUriRequest)) {
if (!Objects.isNull(httpUriRequest.getURI())) {
path = httpUriRequest.getURI().getPath();
targetUrl = httpUriRequest.getURI().toString();
targetHost = httpUriRequest.getURI().getHost();
uri = httpUriRequest.getURI().toString();
}
requestheader = JSON.toJSONString(httpUriRequest.getAllHeaders());
}
if (!Objects.isNull(httpResponse)) {
if (!Objects.isNull(httpResponse.getStatusLine())) {
responseStatus = httpResponse.getStatusLine().getStatusCode();
}
HttpEntity entity = httpResponse.getEntity();
if (!Objects.isNull(entity)) {
try {
//entity实体流保存缓冲区,否则只能操作一次流就会关闭 ,BufferedHttpEntity可以多次读取流
//entity = new BufferedHttpEntity(entity);
responseBody = URLEncodedUtils.parse(entity).toString();
System.out.println(responseBody);
} catch (IOException e) {
LogUtils.error(e, log, "io转换错误");
}
}
responseHeader = JSON.toJSONString(httpResponse.getAllHeaders());
}
RemoteCallLog content = RemoteCallLog.builder()
.type("remote")
.loggerName(HttpsUtils.class.getName())
.level(ex == null ? "INFO" : "ERROR")
.logId(LogUtils.getLogId())
.time(startTime)
.hostName(EnvironmentUtil.getHostName())
.targetMethod(path)
.targetUrl(targetUrl)
.requestId(LogUtils.initRequestIdIfAbsent())
.uri(uri)
.requestMethod(method)
.status(ex == null ? "success" : "fail")
.duration(System.currentTimeMillis() - start)
.requestHeader(requestheader)
.responseBody(responseBody)
.responseHeader(responseHeader)
.stackTrace(ex == null ? "" : ExceptionUtils.getFullStackTrace(ex))
.build();
if ("POST".equals(method)) {
content.setTargetSystem(siteName);
} else {
content.setTargetSystem(targetHost);
}
if (!Objects.isNull(responseStatus)) {
content.setResponseStatus(responseStatus);
}
if ("GET".equals(method) && !Objects.isNull(httpUriRequest) && !Objects.isNull(httpUriRequest.getURI())) {
content.setRequestUrlParam(httpUriRequest.getURI().getQuery());
} else {
HttpPost httpPost = (HttpPost) httpUriRequest;
HttpEntity entity = httpPost.getEntity();
try {
//entity = new BufferedHttpEntity(entity);
content.setRequestBodyParam(URLEncodedUtils.parse(entity).toString());
System.out.println(content.getRequestBodyParam());
} catch (IOException e) {
LogUtils.error(e, log, "io转换错误");
e.printStackTrace();
}
}
return JSONObject.toJSONString(content);
}