Saturday, 15 January 2011

java - HtttpURLConnection never uses gzip -



java - HtttpURLConnection never uses gzip -

i'm developing app, should measure (fairly precisely) size of webpages.

the thing i'm struggling need know sizes of particular files on website. have array of urls , seek fetch headers content-length, files homecoming -1 since chunked. if homecoming -1 seek download them size.

and here lies problem - found out uncompressed version of file.

example file - http://www.google-analytics.com/analytics.js

when open in chrome, headers says this:

however, when download using httpurlconnection, has size of 25421 bytes, , when check content-encoding header, null.

class="lang-java prettyprint-override">connection = (httpurlconnection)(new url(url)).openconnection(); connection.setrequestproperty("accept-encoding", "gzip"); connection.connect(); int contentlength = connection.getcontentlength(); if (contentlength == -1 && connection != null) { inputstream input = connection.getinputstream(); byte[] buffer = new byte[4096]; int count = 0, len; while ((len = input.read(buffer)) > 0) { count += len; } contentlength = count; }

so problem is, download webpage application, , says has (let's say) 400kb. when download using kind of tool, http://tools.pingdom.com/fpt/ , size much smaller, 100kb, since of scripts gzipped, means transfer lower. know 300kb not much, when using mobile transfer, every kb counts, , want app precise.

could point me create mistake, or how solve this? give thanks you

your httpurlconnection setup code looks right me. seek setting user-agent standard browser one, perhaps server trying more intelligent ought be. failing that, run traffic through debugging proxy fiddler or burp see what's going on @ network level.

java android http

No comments:

Post a Comment