er4*_*z0r 5 java size http chunked-encoding content-length
我想知道响应某个http请求发送了多少数据.我目前做的是这样的:
HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
Run Code Online (Sandbox Code Playgroud)
//检查content-size的响应int feedsize = con.getContentLength();
问题是,内容 - legnth并不总是设置.例如,当服务器使用transfer-encoding = chunked时,我得到一个值-1.
我并不需要这显示进度信息.我只需要知道完成后发送给我的数据的大小.
背景:我需要这些信息,因为我想将它与使用gzip编码发送的响应大小进行比较.
我会使用一个commons-io CountingInputStream,它可以帮你完成任务.一个完整但微不足道的例子:
public long countContent(URL feedurl) {
CountingInputStream counter = null;
try {
HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
counter = new CountingInputStream(con.getInputStream());
String output = IOUtils.toString(counter);
return counter.getByteCount();
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
IOUtils.closeQuietly(counter);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7444 次 |
| 最近记录: |