【发布时间】:2010-07-08 11:52:59
【问题描述】:
我如何从服务器读取文件以某个偏移量开始(类似于 wget -c 的行为)?我必须将哪些标头发送到服务器?服务器必须支持哪些期货?
【问题讨论】:
我如何从服务器读取文件以某个偏移量开始(类似于 wget -c 的行为)?我必须将哪些标头发送到服务器?服务器必须支持哪些期货?
【问题讨论】:
您应该在请求中使用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。
【讨论】:
在http://www.gnu.org/software/wget/manual/wget.html
请注意,“-c”仅适用于 ftp 服务器和 http 服务器 支持 Range 标头。
在https://www.rfc-editor.org/rfc/rfc2616
字节范围说明符的示例 值(假设实体主体为
长度 10000):- The first 500 bytes (byte offsets 0-499, inclusive): bytes=0- 499 - The second 500 bytes (byte offsets 500-999, inclusive): bytes=500-999 - The final 500 bytes (byte offsets 9500-9999, inclusive): bytes=-500 - Or bytes=9500- - The first and last bytes only (bytes 0 and 9999): bytes=0-0,-1 - Several legal but not canonical specifications of the second500 字节(字节偏移 500-999,包括): 字节=500-600,601-999 字节=500-700,601-999
所以你应该发送
Range:bytes=9500-
要测试服务器是否支持它,您可以测试接受范围
接受字节范围请求的源服务器可以发送
接受范围:字节
但不是必须这样做。客户端可以生成字节范围 未收到资源的此标头的请求 涉及。范围单位在 3.12 节中定义。
不接受任何范围请求的服务器 资源可以发送
Accept-Ranges: none建议客户端不要尝试范围请求。
【讨论】: