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
- header中标明了
Content-Encoding
为gzip - 有body
移除 Content-Encoding
、Content-Length
,并对结果进行解压缩。
可以看到以上逻辑完成了:
- 开发者没有添加
Accept-Encoding
时,自动添加Accept-Encoding: gzip
- 自动添加的request,response支持自动解压
- 手动添加不负责解压缩
- 自动解压时移除
Content-Length
,所以上层Java代码想要contentLength时为-1 - 自动解压时移除
Content-Encoding
- 自动解压时,如果是分块传输编码,
Transfer-Encoding: chunked
不受影响。
以上6点是我们通过OkHttp源码得出的结论,我们以此来继续看下其他框架。
HttpUrlConnection
1. 是否自动添加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
。
2. 自动添加的request,response是否支持自动解压
By default, this implementation of HttpURLConnection requests that servers use gzip compression and it automatically decompresses the data for callers of getInputStream().
即返回的数据是已经自动解压缩的。
3. 手动添加是否负责解压缩
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,只要是显式声明过,都不会再处理,即:手动添加不会负责解压缩。
4. 自动解压时Content-Length问题
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是否还继续成立呢?
实测后发现 :
- 4.4之后的版本,
Content-Length
被移除,getContentLength()
= -1 - 2.3- 4.3之间,
Content-Length
没有移除,getContentLength()
= compressed size
5. 自动解压时的Content-Encoding
与Content-Length对应:
- 4.4之后的版本,
Content-Encoding
被移除 - 2.3- 4.3之间,
Content-Encoding
存在,无变化。
6. 自动解压时的分块编码传输
与OkHttp相同,Transfer-Encoding: chunked
不受影响。
Apache
这里不再赘述,仅阐述结论:
无自动添加、解压机制。
1、是否支持自动添加Accept-Encoding
与数据自动解压?
OkHttp | yes |
HttpUrlConnection | yes |
Apache | no |
2、支持自动后,response header的表现如何?
OkHttp | 被移除 | 被移除 | -1 |
HttpUrlConnection(2.3 ~ 4.3) | 不变 | 不变 | compressed size |
HttpUrlConnection(4.4 ~ ?) | 被移除 | 被移除 | -1 |
OkHttp | 被移除 | 不变 |
HttpUrlConnection(2.3 ~ 4.3) | 不变 | 不变 |
HttpUrlConnection(4.4 ~ ?) | 被移除 | 不变 |
3、自动模式启动后,在Java中获取contentLength无论是哪个版本的HttpUrlConnection还是OkHttp都是不可信的,都不是解压缩之后的值(可能为-1或compressed size),因此最好不要通过contentLength来做什么操作。
4、HttpUrlConnection、OkHttp均是手动添加不自动解压缩,Apache没有自动添加自动解压功能。三者在手动添加Accept-Encoding
后,表现一致(利用这个特点,可以做一个在三者之上的网络框架,随意切换三种通道)。
<br />幸好java.util.zip包下提供了GZIPxxxx<br />可以直接调用,<br />ContentEncoding为text/html或其他类似的文本直接打印出来就行<br />为gzip的话,需要再转化一下<br />上程序:<br />String url = "http://health.sohu.com/yangshengtang/";
URL cumtURL = new URL(
url);
HttpURLConnection cum
对于HTTP的报文头设置,作为一名Android开发者,如果在没有使用第三方框架的前提条件下,直接使用HttpUrlConnection进行GET数据,会因为参数属性的设置导致一些不必要的麻烦。本文主要是我在裸辞后,找工作时,公司给的测试题,在使用HttpURLConnection访问数据时挖出的坑。共有两个大方向的BUG。一是GZIPInputStream,另一个是Unicode。
一、GZIP
进入主题之前,我们先来看一下客户端与服务器通信过程中,如果服务器支持,HTTP gzip压缩是如何实现的?如图所示: request header中声明Accept-Encoding: gzip,告知服务器客户端接受gzip的数据。 服务器支持的情况下,返回gzip后的response body,同时加入以下header:Content-Encoding: gzip:表明body是gzip过的数据...
出处:http://www.cnblogs.com/ct2011/p/5835990.html
进入主题之前,我们先来看一下客户端与服务器通信过程中,如果服务器支持,HTTP gzip压缩是如何实现的?
如图所示:
request header中声明Accept-Encoding: gzip,...
原文链接:https://blog.csdn.net/lianzhang861/article/details/80840242
代码在原文的基础上增加了Transfer-Encoding的判断。
最近项目在使用 http://wthrcdn.etouch.cn/weather_mini
获取天气预报数据,但此接口只能用get方法请求,而且返回数据一直是乱码,原来返回数据用gzip格式压缩了,所以我的方
法中增加了判断是否为gzip并解压
public static String sendGet(S
gzip是一种数据格式,默认且目前仅使用deflate算法压缩data部分;
Gzip是一种流行的文件压缩算法,现在的应用十分广泛,尤其是在Linux平台。当应用Gzip压缩到一个纯文本文件时,效果是非常明显的,大约可以减少70%以上的文件大小。这取决于文件中的内容。
利用Apache中的Gzip模块,我们可以使用Gzip压缩算法来对Apache服务器发布的
本节引言:前面两节我们学习的都是一些概念性的东西,Http的协议以及协议头的一些东东,而本节我们就要堆码了,而本节学习的是Android为我们提供的Http请求方式之一:HttpURLConnection,除了这种,还有一种还有一种HttpClient,后者我们会下一节讲!不过前者一旦请求复杂起来,使用起来非常麻烦,而后者我们Java抓包也经常会用到,是Apache的,毕竟不是谷歌亲儿子,而在4....
1、httpclient请求头部添加 gzip支持httpRequest.addHeader(“Accept-Encoding”, “gzip”);举例post上传params:因为参数数据多,可以将参数转换成string,然后利用GZIPOutputStream进行压缩try {List nameValuePairs = params.getBodyParams();StringBuilder ...
我试图获取从URLConnection获得的数据长度.由于我正在测量要传输的数据量,因此我不想知道未压缩数据的大小,而是要知道压缩后的数据的大小.不幸的是InputStream会自动解压缩gzip压缩的数据.我必须手动下载整个文件,以防输出分块并且无法通过connection.getContentLength()获得长度.代码在这里try {connection = (HttpURLConnect...
2、调用URL对象的openConnection()方法,创建HttpURLConnection对象;
3、调用HttpURLConnection对象setDoOutput(true)...
介绍Android中Http请求方式的选择、区别及几个常用框架对API的选择1. 两种请求方式对比Android Http请求API主要分两种:第一种是Java的HttpURLConnection,默认带gzip压缩第二种Apache的HttpClient,默认不带gzip压缩两种方式请求connection都是keep alive,默认User-Agent不同。关于两种方式发出去的请求头对比图如...