从服务器读取一些偏移量的文件 [英] read file from server with some offset
问题描述
如何从服务器读取文件,从一些偏移开始(与 wget -c 类似的行为)?我必须向服务器发送哪些标头?服务器支持哪些期货?
How can I read file from server starting with some offset (Similar behavior to wget -c)? What headers I must send to server? What futures must server support?
推荐答案
您应该使用范围
标题在请求中。但是,只有当服务器通过 Accept-Ranges
响应标题通知您它接受范围请求时,您才可以使用它。
You should use the Range
header in the request. But you may use it only if the server informs you that it accept range request by Accept-Ranges
response header.
这是一个示例会话。假设我们有兴趣获得此图片的一部分。首先,我们发送HTTP HEAD
请求以确定:a)服务器是否支持字节范围,b)内容长度:
This is an example session. Suppose we are interested in getting a part of this picture. First, we send a HTTP HEAD
request to determine: a) if the server supports byte ranges, b) the content-length:
> 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
接下来,我们发送 GET
请求范围
标头询问图片的前11个字节:
Next, we send a GET
request with the Range
header asking for the first 11 bytes of the picure:
> 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个字节的十六进制转储:
This is a hex dump of the first 11 bytes:
00000000 ff d8 ff e0 00 10 4a 46 49 46 00 |......JFIF.|
0000000b
有关详细信息,请参阅范围标题规范。
For more info see the Range header specification in HTTP RFC 2616.
这篇关于从服务器读取一些偏移量的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!