Get Size of HTTP Response in Java
Asked Answered
S

2

4

I would like to know how much data was sent in response to a certain http-request. What I currently do is this:

   HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();

//check the response for the content-size int feedsize = con.getContentLength();

The problem is, that content-legnth is not always set. E.g. when the server uses transfer-encoding=chunked I get back a value of -1.

I do not need this to display progress information. I just need to know the size of the data that was sent to me after it has been done.

Background: I need this information because I would like to compare it to the size of a response, that was sent using gzip encoding.

Subclass answered 4/11, 2009 at 14:35 Comment(0)
K
8

I'd use a commons-io CountingInputStream, which would do the job for you. A full but trivial example:

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);
  }
}
Katharinakatharine answered 4/11, 2009 at 14:43 Comment(1)
Thanks that is exactly what I needed. And with maven one more dependencies won't hurt ;-)Subclass
L
2

You can extend FilterInputStream, overriding the read(), read(byte[],int,int), and skip methods so that after calling the super form, they update a counter with the number of bytes read.

Then wrap the input stream returned by URLConnection with one of these, and use the wrapper in place of the original stream. When you're done, you can query wrapper's its counter.

Other ("manual") approaches would be use a tool like YSlow to gather statistics in a browser, or Wireshark to examine the traffic on the network.

Lajoie answered 4/11, 2009 at 14:42 Comment(1)
Thanks for your reply! I must admit, that I am not a very good and also a very lazy coder. I don't like to invent my own wheel/hack for this. So to avoid mistakes and save time I will use the CountingInputStream from commons-io as an established solution. Thanks again for your time!Subclass

© 2022 - 2024 — McMap. All rights reserved.