您应该在请求中使用Range标头.但是,只有当服务器通过Accept-Ranges响应标头通知您它接受范围请求时,才可以使用它.
这是一个示例会话.假设我们有兴趣获取this picture的一部分.首先,我们发送HTTP HEAD请求以确定:a)如果服务器支持字节范围,b)内容长度:
> HEAD /2238/2758537173_670161cac7_b.jpg HTTP/1.1
> Host: farm3.static.flickr.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 08 Jul 2010 12:22:12 GMT
< Content-Type: image/jpeg
< Connection: keep-alive
< Server: Apache/2.0.52 (Red Hat)
< Expires: Mon, 28 Jul 2014 23:30:00 GMT
< Last-Modified: Wed, 13 Aug 2008 06:13:54 GMT
< Accept-Ranges: bytes
< Content-Length: 350015
接下来,我们发送一个带有Range标头的GET请求,询问图片的前11个字节:
> GET /2238/2758537173_670161cac7_b.jpg HTTP/1.1
> Host: farm3.static.flickr.com
> Accept: */*
> Range: bytes=0-10
>
< HTTP/1.1 206 Partial Content
< Date: Thu, 08 Jul 2010 12:26:54 GMT
< Content-Type: image/jpeg
< Connection: keep-alive
< Server: Apache/2.0.52 (Red Hat)
< Expires: Mon, 28 Jul 2014 23:30:00 GMT
< Last-Modified: Wed, 13 Aug 2008 06:13:54 GMT
< Accept-Ranges: bytes
< Content-Range: bytes 0-10/350015
< Content-Length: 11
<
这是前11个字节的十六进制转储:
00000000 ff d8 ff e0 00 10 4a 46 49 46 00 |......JFIF.|
0000000b
有关详细信息,请参阅HTTP RFC 2616中的Range header specification.