简体   繁体   English

如何使用 Jetty 客户端根据请求获取 bytesIn/bytesOut

[英]How to get the bytesIn/bytesOut on a request basis using Jetty Client

I am using Jetty Client 11 and would like to compute for each request the sent/received bytes.我正在使用 Jetty Client 11 并希望为每个请求计算发送/接收的字节。

I am using High Level HttpClient.我正在使用高级 HttpClient。 I have read this documentation but I don't see any information on how to do that:我已阅读此文档,但没有看到有关如何执行此操作的任何信息:

I see there is a way to have this information by doing:我看到有一种方法可以通过以下方式获取这些信息:

        ConnectionStatistics connectionStatistics = new ConnectionStatistics();
        httpClient.addBean(connectionStatistics);

Then:然后:

        Request request = httpClient.newRequest(uri)
                .version(HttpVersion.HTTP_2)
                .timeout(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        request.send(listener);
        ConnectionStatistics connectionStatistics = httpClient.getBean(ConnectionStatistics.class);
        System.out.println(connectionStatistics.getSentBytes()+"/"+ connectionStatistics.getReceivedBytes());

And I must be doing something wrong as it does not even work on a global basis, it always gives 0.而且我一定做错了什么,因为它甚至不能在全球范围内工作,它总是给出 0。

And anyway, I don't see how to make it work on a request basis.无论如何,我不知道如何让它在请求的基础上工作。

EDIT:编辑:

I tried @sbordet answer:我试过@sbordet 答案:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);

and it works fine for response.它适用于响应。 BUT for request, I would like to measure the bytes sent, it always give 0 (Note I only have gets without Content) while I would expect to have the raw size (the Raw HTTP request bytes including header size...)但是对于请求,我想测量发送的字节,它总是给出 0(注意我只有没有内容的获取),而我希望有原始大小(原始 HTTP 请求字节,包括 header 大小......)

You can count request bytes using Request.ContentListener and response bytes using Response.ContentListener :您可以使用Request.ContentListener计算请求字节数,使用Response.ContentListener计算响应字节数:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM