【问题标题】:OKHTTP: How to measure total bytes in Request and Response?OKHTTP:如何测量请求和响应中的总字节数?
【发布时间】:2017-12-17 22:16:08
【问题描述】:

使用okhttp3POST 一个JSON 有效负载到Web 服务器。我们想知道请求的总字节数和POST 请求的响应。最终,我们希望获得数据吞吐量。代码如下:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
    .url(url)
    .post(body)
    .build();    
mBytesSent += request.body.contentLength();  // Payload byte count
mBytesSent += request.headers().size();
mBytesSent += request.tag().toString().length();

Response response = client.newCall(request).execute()  

mBytesRecd += response.body().contentLength();
mBytesRecd += response.headers().toString().length();
mBytesRecd += response.message().length();

这是POST 的完整字节数吗?

【问题讨论】:

  • request.tag().toString().length()response.message().length() 这些方法返回什么?
  • 从技术上讲,不包括底层 TCP 标头,headers + body 将为您提供总字节数。
  • 是否有专门的调用来返回发送或接收的总字节数?
  • 您可以在中间放置一个代理/人,例如 Pipe Server github.com/QuickServerLab/QuickServer-Examples 或 jmeter 记录器进行检查
  • 我问这个问题的原因是,我们发现通过 LTE 数据连接的流量是这个估计值的 5 倍。我正在寻找差异。有没有办法访问 Android 传输 Sent/Recd 计数器?

标签: java okhttp3


【解决方案1】:

您可以调整 HttpLoggingInterceptor 中的代码,该代码缓冲流式请求和响应以获取它们的大小,即使事先不知道。

https://github.com/square/okhttp/blob/master/okhttp-logging-interceptor/src/main/java/okhttp3/logging/HttpLoggingInterceptor.java

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-03-31
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 2017-08-22
  • 2019-08-27
相关资源
最近更新 更多