进入主题以前,咱们先来看一下客户端与服务器通讯过程当中,若是服务器支持,HTTP gzip压缩是如何实现的?github
如图所示:apache
request header中声明Accept-Encoding: gzip
,告知服务器客户端接受gzip的数据。
服务器支持的状况下,返回gzip后的response body,同时加入如下header:服务器
Content-Encoding: gzip
:代表body是gzip过的数据Content-Length:117
:表示body gzip压缩后的数据大小,便于客户端使用。Transfer-Encoding: chunked
:分块传输编码OK,HTTP gzip压缩的基本流程咱们理清楚了,来看在Android各网络框架中表现有什么差别。网络
OkHttp做为目前Android最火的网络库,应用范围较广,相比于Android自带的HttpUrlConnection、Apache坑也少不少。
咱们首先来看这个库的实现:
(注:如下代码基于OkHttp 3.4.1, 以前的版本逻辑也是同样的,但3.4.0开始将这些逻辑抽离到了内置的interceptor中,看起来较为方便)框架
// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing // the transfer stream. boolean transparentGzip = false; if (userRequest.header("Accept-Encoding") == null) { transparentGzip = true; requestBuilder.header("Accept-Encoding", "gzip"); }
若是header中没有Accept-Encoding
,默认自动添加 ,且标记变量transparentGzip
为true
。
if (transparentGzip && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding")) && HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); Headers strippedHeaders = networkResponse.headers().newBuilder() .removeAll("Content-Encoding") .removeAll("Content-Length") .build(); responseBuilder.headers(strippedHeaders); responseBuilder.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody))); }
针对返回结果,若是同时知足如下三个条件:
transparentGzip
为true
,即以前自动添加了Accept-Encoding
Content-Encoding
为gzip移除 Content-Encoding
、Content-Length
,并对结果进行解压缩。
能够看到以上逻辑完成了:
Accept-Encoding
时,自动添加Accept-Encoding: gzip
Content-Length
,因此上层Java代码想要contentLength时为-1Content-Encoding
Transfer-Encoding: chunked
不受影响。以上6点是咱们经过OkHttp源码得出的结论,咱们以此来继续看下其余框架。
Accept-Encoding: gzip
官网有过说明:
In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:
Accept-Encoding: gzip
Take advantage of this by configuring your Web server to compress responses for clients that can support it. If response compression is problematic, the class documentation shows how to disable it.
即:2.3后默认是gzip,不加Accept-Encoding
会被自动添加上Accept-Encoding: gzip
。
By default, this implementation of HttpURLConnection requests that servers use gzip compression and it automatically decompresses the data for callers of getInputStream().
即返回的数据是已经自动解压缩的。
By default, this implementation of HttpURLConnection requests that servers use gzip compression and it automatically decompresses the data for callers of getInputStream(). The Content-Encoding and Content-Length response headers are cleared in this case. Gzip compression can be disabled by setting the acceptable encodings in the request header:
urlConnection.setRequestProperty("Accept-Encoding", "identity");
Setting the Accept-Encoding request header explicitly disables automatic decompression and leaves the response headers intact; callers must handle decompression as needed, according to the Content-Encoding header of the response.
例子中只提到设置为identity
时能够禁止gzip压缩。
可是请注意最后一段提到,显式声明会禁止自动解压,同时保留header完整性,须要根据Content-Encoding
来本身处理response。
实测4.1 - 6.0 版本以后发现,并非非要指定identity才能屏蔽,指定gzip同样也不会解压缩。so,只要是显式声明过,都不会再处理,即:手动添加不会负责解压缩。
Since HTTP’s Content-Length header returns the compressed size, it is an error to use getContentLength() to size buffers for the uncompressed data. Instead, read bytes from the response until InputStream.read() returns -1.
即:getContentLength()
值为gzip压缩时的数据大小。
以前提到OkHttp在处理gzip压缩时会把Content-Length
移除,contentLength在Java层获取为-1,而HttpURLConnection 在Android 4.4之后底层是由OkHttp实现的,那文档中提到的getContentLength()
是compressed size是否还继续成立呢?
实测后发现 :
Content-Length
被移除,getContentLength()
= -1Content-Length
没有移除,getContentLength()
= compressed size与Content-Length对应:
Content-Encoding
被移除Content-Encoding
存在,无变化。与OkHttp相同,Transfer-Encoding: chunked
不受影响。
这里再也不赘述,仅阐述结论:
无自动添加、解压机制。
一、是否支持自动添加Accept-Encoding
与数据自动解压?
name | transparent response compression |
---|---|
OkHttp | yes |
HttpUrlConnection | yes |
Apache | no |
二、支持自动后,response header的表现如何?
name | Content-Encoding: gzip | Header : Content-Length | Java : ContentLength |
---|---|---|---|
OkHttp | 被移除 | 被移除 | -1 |
HttpUrlConnection(2.3 ~ 4.3) | 不变 | 不变 | compressed size |
HttpUrlConnection(4.4 ~ ?) | 被移除 | 被移除 | -1 |
或
name | Content-Encoding: gzip | Transfer-Encoding: chunked |
---|---|---|
OkHttp | 被移除 | 不变 |
HttpUrlConnection(2.3 ~ 4.3) | 不变 | 不变 |
HttpUrlConnection(4.4 ~ ?) | 被移除 | 不变 |
三、自动模式启动后,在Java中获取contentLength不管是哪一个版本的HttpUrlConnection仍是OkHttp都是不可信的,都不是解压缩以后的值(可能为-1或compressed size),所以最好不要经过contentLength来作什么操做。
四、HttpUrlConnection、OkHttp均是手动添加不自动解压缩,Apache没有自动添加自动解压功能。三者在手动添加Accept-Encoding
后,表现一致(利用这个特色,能够作一个在三者之上的网络框架,随意切换三种通道)。