27

Java 9 中实验性引入的HttpClient现在在 Java 11 中稳定了,但毫不奇怪,似乎很少有项目真正使用它。文档几乎不存在。

进行 HTTP 调用时最常见的问题之一是记录请求/响应。当然,您将如何使用 来做到这一点HttpClient,而不是在每次调用中手动记录它?是否有类似所有其他 HTTP 客户端提供的拦截器机制?

4

3 回答 3

23

-Djdk.httpclient.HttpClient.log=requests您可以通过在 Java 命令行上指定来记录请求和响应。

至于测试/模拟你可能想看看离线测试: http ://hg.openjdk.java.net/jdk/jdk/file/tip/test/jdk/java/net/httpclient/offline/

根据您要实现的目标,您也可以使用“DelegatingHttpClient”来拦截和记录请求和响应。

除了 Java API 文档之外,还有一些高级文档位于http://openjdk.java.net/groups/net/httpclient/index.html

附加说明:

jdk.httpclient.HttpClient.log属性是一个特定于实现的属性,其值是一个逗号分隔的列表,可以在 Java 命令行上配置,用于诊断/调试目的,具有以下值:

-Djdk.httpclient.HttpClient.log=
       errors,requests,headers,
       frames[:control:data:window:all],content,ssl,trace,channel,all
于 2018-11-09T17:58:59.927 回答
13

如果我们查看jdk.internal.net.http.common.DebugLogger源代码,我们可以看到一些使用logger 的 logger,这些 loggerSystem.Logger又将System.LoggerFinder用于选择 logger 框架。JUL 是默认选择。记录器名称是:

  • jdk.internal.httpclient.debug
  • jdk.internal.httpclient.websocket.debug
  • jdk.internal.httpclient.hpack.debug

可以通过将它们设置为系统属性来启用它们。例如运行-Djdk.internal.httpclient.debug=true将产生:

DEBUG: [main] [147ms] HttpClientImpl(1) proxySelector is sun.net.spi.DefaultProxySelector@6dde5c8c (user-supplied=false)
DEBUG: [main] [183ms] HttpClientImpl(1) ClientImpl (async) send https://http2.github.io/ GET
DEBUG: [main] [189ms] Exchange establishing exchange for https://http2.github.io/ GET,
     proxy=null
DEBUG: [main] [227ms] PlainHttpConnection(?) Initial receive buffer size is: 43690
DEBUG: [main] [237ms] PlainHttpConnection(SocketTube(1)) registering connect event
DEBUG: [HttpClient-1-SelectorManager] [239ms] SelectorAttachment Registering jdk.internal.net.http.PlainHttpConnection$ConnectEvent@354bf356 for 8 (true)
...
于 2018-11-08T20:13:04.100 回答
6

在我们这边,我们没有发现-Djdk.internal.httpclient.debug足够可读的日志记录。我们想出的解决方案是用一个装饰器包装 HttpClient,该装饰器将能够拦截调用并提供日志记录。这里它看起来如何(不仅应该为方法做sendsendAsync

public class HttpClientLoggingDecorator extends HttpClient {

  private static final Logger logger = Logger.getLogger(HttpClientLoggingDecorator.class.getName());

  private final HttpClient client;

  ...

  @Override
  public <T> HttpResponse<T> send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
    throws IOException,
      InterruptedException
  {
    subscribeLoggerToRequest(req);

    HttpResponse<T> response = client.send(req, responseBodyHandler);

    logResponse(response);
    return response;
  }

  private void subscribeLoggerToRequest(HttpRequest req) {
    // define a consumer for how you want to log
    // Consumer<String> bodyConsumer = ...;
    if (req.bodyPublisher().isPresent()) {
      req.bodyPublisher()
              .ifPresent(bodyPublisher -> bodyPublisher.subscribe(new HttpBodySubscriber(bodyConsumer)));
    } else {
      bodyConsumer.accept(NO_REQUEST_BODY);
    }
  }

  private <T> void logResponse(HttpResponse<T> response) {
    // String responseLog = ...;
    logger.info(responseLog);
  }

}

这是HttpBodySubscriber

public class HttpBodySubscriber implements Flow.Subscriber<ByteBuffer> {

  private static final long UNBOUNDED = Long.MAX_VALUE;

  private final Consumer<String> logger;

  public HttpBodySubscriber(Consumer<String> logger) {
    this.logger = logger;
  }

  @Override
  public void onSubscribe(Flow.Subscription subscription) {
    subscription.request(UNBOUNDED);
  }

  @Override
  public void onNext(ByteBuffer item) {
    logger.accept(new String(item.array(), StandardCharsets.UTF_8));
  }

  @Override
  public void onError(Throwable throwable) {
  }

  @Override
  public void onComplete() {
  }

}
于 2019-04-01T12:52:17.287 回答