初次认识:Transfer-Encoding: chunked

Transfer-Encoding: chunked 表示输出的内容长度不能确定,普通的静态页面、图片之类的基本上都用不到这个。

但动态页面就有可能会用到,但我也注意到大部分asp,php,asp.net动态页面输出的时候大部分还是使用Content-Length,没有使用Transfer-Encoding: chunked。

不过如果结合:Content-Encoding: gzip 使用的时候,Transfer-Encoding: chunked还是比较有用的。

记得以前实现:Content-Encoding: gzip 输出时,先把整个压缩后的数据写到一个很大的字节数组里(如 ByteArrayOutputStream),然后得到数组大小 -> Content-Length。

如果结合Transfer-Encoding: chunked使用,就不必申请一个很大的字节数组了,可以一块一块的输出,更科学,占用资源更少。

发现淘宝网使用Transfer-Encoding: chunked的次数较多。

下面是一个标准的 Header 

HTTP/1.1 200 OK
Server: Tengine
Date: Mon, 05 Sep 2011 14:30:44 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 27 May 2010 07:19:58 GMT
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Expires: Thu, 02 Sep 2021 14:30:44 GMT
Cache-Control: max-age=315360000
Content-Encoding: gzip

<chunk-size>    (其中,chunk-size = chunk-data.getBytes().length,不包含 )

<chunk-data>

<chunk-size>  

<chunk-data>

<0>   (chunk 结束)

-----------------------------------------------------------

下面是一个截图:

原文地址:https://www.cnblogs.com/personnel/p/4583098.html