Java HttpURLConnection VS Android HttpURLConnection

I have this simple code that runs on a Windows virtual machine:

URL url = new URL("http:xxx.xxx.xxx.xxx/api/cities.json"); HttpURLConnection connexion = (HttpURLConnection) url.openConnection(); connexion.setRequestMethod("GET"); System.out.println("response " + connexion.getResponseCode()); connexion.connect(); InputStream is = connexion.getInputStream(); for (String header : connexion.getHeaderFields().keySet()) { System.out.println(header + ":" + connexion.getHeaderField(header)); } 

Providing these headers

 response 200 null:HTTP/1.1 200 OK X-Frame-Options:SAMEORIGIN Access-Control-Allow-Origin:* Vary:Accept-Encoding Date:Sun, 23 Mar 2014 03:00:06 GMT Content-Length:1894 Connection:keep-alive Content-Type:application/json Server:nginx 

And the same Android code giving abstracts

 03-22 23:10:35.405: I/System.out(23707): null:HTTP/1.1 200 OK 03-22 23:10:35.405: I/System.out(23707): Access-Control-Allow-Origin:* 03-22 23:10:35.405: I/System.out(23707): Connection:keep-alive 03-22 23:10:35.405: I/System.out(23707): Content-Type:application/json 03-22 23:10:35.405: I/System.out(23707): Date:Sun, 23 Mar 2014 03:10:35 GMT 03-22 23:10:35.405: I/System.out(23707): Server:nginx 03-22 23:10:35.405: I/System.out(23707): Vary:Accept-Encoding 03-22 23:10:35.405: I/System.out(23707): X-Android-Received-Millis:1395544235409 03-22 23:10:35.405: I/System.out(23707): X-Android-Response-Source:NETWORK 200 03-22 23:10:35.405: I/System.out(23707): X-Android-Selected-Transport:http/1.1 03-22 23:10:35.405: I/System.out(23707): X-Android-Sent-Millis:1395544235363 03-22 23:10:35.405: I/System.out(23707): X-Frame-Options:SAMEORIGIN 

I just can't find the content length header. Did I miss something?

+6
source share
1 answer

Android HttpURLConnection does some magic transparently for you, like caching and gzip processing.

So, if you do not install Accept-Encoding: identity , HUC sets Accept-Encoding: gzip,deflate . And since nginx is a smart, nice thing, it can pass your gzip response in encoding. You did not tell the HUC to give the answer as gzipped for you, but it unpacks that answer for you and resets the header as it is misleading.

You can see this as a powerful feature to save bandwidth and power consumption without Android programs dealing with gzip or deflate encoding.

If you simply pass the response to your JSON parser, you will receive uncompressed data in the correct size, although you do not know the size in advance.

You can verify what I said by doing wireshark or tcpdump on the server side, or if you have an Android root device, there are ways to do this on the client side as well.

+9
source

All Articles