对Httpclient的支持
目前TLog对于Httpclient客户端的支持是有侵入性的,对于请求端需要加入拦截器,把日志标签信息记录用过Http Header传递到服务器。
HttpClient 4.X 添加拦截器TLogHttpClientInterceptor
代码示例:
String url = "http://127.0.0.1:2111/hi?name=2323";
CloseableHttpClient client = HttpClientBuilder.create()
.addInterceptorFirst(new TLogHttpClientInterceptor())
.build();
HttpGet get = new HttpGet(url);
try {
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
log.info("http response code:{}", response.getStatusLine().getStatusCode());
log.info("http response result:{}",entity == null ? "" : EntityUtils.toString(entity));
} catch (IOException e) {
e.printStackTrace();
}
HttpClient 5.X 添加拦截器TLogHttpClient5Interceptor
String url = "http://127.0.0.1:2111/hi?name=2323";
CloseableHttpClient client = HttpClientBuilder.create()
.addInterceptorFirst(new TLogHttpClient5Interceptor())
.build();
HttpGet get = new HttpGet(url);
try {
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
log.info("http response code:{}", response.getStatusLine().getStatusCode());
log.info("http response result:{}",entity == null ? "" : EntityUtils.toString(entity));
} catch (IOException e) {
e.printStackTrace();
}
帮助我们改善文档 (opens new window)
last update: 2022/09/21, 16:00:22