Android HttpURLConnection Accept-Encoding: gzip 版本问题

来源:互联网 发布:tripmode for mac 编辑:程序博客网 时间:2024/06/11 22:45

Android 官方文档对 HttpURLConnection 的 Performance 的描述有一段:

By default, this implementation of HttpURLConnection requests that servers use gzip compression

参见: http://developer.android.com/reference/java/net/HttpURLConnection.html

就是说 使用HttpURLConnection发请求时,默认的request hearder里会加上 Accept-Encoding: gzip

这是坑!!!

因为Android的这个Feature是在Gingerbread(android 2.3.3) 版本以后才加上的, 而这一点并未在其官方API中注明。

所以同样的应用,在低版本的android上,发出的是非gzip的请求,而在高版本的android上发出的是gzip的请求,这样在server端处理时,就要特别小心兼容两类情况。

(我们因此出了一个大bug。)

下面是另外一篇文章中提到的:

In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:Accept-Encoding: gzip
参见: http://android-developers.blogspot.com/2011/09/androids-http-clients.html


如果不想处理这些问题,也可以弃用 HttpURLConnection 而改用 Apache Http Client, 它发送的都是非gzip类型的请求,与android版本无关。


0 0