首页 > 模拟请求一张图片,请求首部包含range,但为什么返回的没有content-range字段?

模拟请求一张图片,请求首部包含range,但为什么返回的没有content-range字段?

这是请求头

GET http://subversion.apache.org/images/windows.png HTTP/1.1
Host: subversion.apache.org
Range: 1000-3000
Content-Length: 14
Pragma: no-cache

这是响应头

HTTP/1.1 200 OK
Date: Sun, 28 Aug 2016 01:33:56 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 14 Jan 2010 21:29:52 GMT
ETag: "1403-47d2696640237"
Accept-Ranges: bytes
Content-Length: 5123
Content-Type: image/png
Cache-Control: no-cache

他直接返回了一个完整的图片,按照预想应该是返回部分图片才对


改为Range: bytes=1000-3000即可。

注意上面的写法你会得到共2001字节的数据,因为Range是包括头尾的

根据RFC2616,Range标头需要写单位,格式定义如下

   ranges-specifier = byte-ranges-specifier
   byte-ranges-specifier = bytes-unit "=" byte-range-set
   byte-range-set  = 1#( byte-range-spec | suffix-byte-range-spec )
   byte-range-spec = first-byte-pos "-" [last-byte-pos]
   first-byte-pos  = 1*DIGIT
   last-byte-pos   = 1*DIGIT

byte-ranges-specifier的取值例子(假定消息体完整长度为10000):

另外,为了确保分块下载的时候不会正好遇到服务器更新而下载到脏数据,你还是应该把ETag发到服务器端,或者加上标头下载。

【热门文章】
【热门文章】