Http协议内容长度
我正在开发一个简单的下载应用程序。 在请求以下文件时,Firefox 和我的应用程序都没有获取内容长度字段。 但是如果我使用 wget 服务器发出请求,则会发送内容长度字段。 我确实更改了 wgets 用户代理字符串进行测试,但它仍然具有内容长度字段。
有什么想法为什么会发生这种情况吗?
wget request
---request begin---
GET /dc-13/video/2005_Defcon_V2-P_Zimmerman-Unveiling_My_Next_Big_Project.mp4 HTTP/1.0
User-Agent: test
Accept: */*
Host: media.defcon.org
Connection: Keep-Alive
---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.0 200 OK
Server: lighttpd
Date: Sun, 05 Apr 2009 04:40:08 GMT
Last-Modified: Tue, 23 May 2006 22:18:19 GMT
Content-Type: video/mp4
Content-Length: 104223909
Connection: keep-alive
firefox request
GET /dc-13/video/2005_Defcon_V2-P_Zimmerman-Unveiling_My_Next_Big_Project.mp4 HTTP/1.1
Host: media.defcon.org
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.0.8) Gecko/2009032608 Firefox/3.0.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.defcon.org/html/links/defcon-media-archives.html
Pragma: no-cache
Cache-Control: no-cache
HTTP/1.x 200 OK
Server: lighttpd
Date: Sun, 05 Apr 2009 05:20:12 GMT
Last-Modified: Tue, 23 May 2006 22:18:19 GMT
Content-Type: video/mp4
Transfer-Encoding: chunked
更新:
是否有一个我可以发送的标头,它会告诉 Lighthttpd 不要使用分块编码。我原来的问题是我正在使用 urlConnection
来获取我的 java 应用程序中的文件自动发送 HTTP 1.1 请求。
我想知道文件的大小,以便我可以更新我的百分比。
I am working on a simple download application. While making a request for the following file both firefox and my application doesn't get the content-length field. But if i make the request using wget server does send the content-length field. I did change wgets user agent string to test and it still got the content-length field.
Any ideas why this is happening?
wget request
---request begin---
GET /dc-13/video/2005_Defcon_V2-P_Zimmerman-Unveiling_My_Next_Big_Project.mp4 HTTP/1.0
User-Agent: test
Accept: */*
Host: media.defcon.org
Connection: Keep-Alive
---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.0 200 OK
Server: lighttpd
Date: Sun, 05 Apr 2009 04:40:08 GMT
Last-Modified: Tue, 23 May 2006 22:18:19 GMT
Content-Type: video/mp4
Content-Length: 104223909
Connection: keep-alive
firefox request
GET /dc-13/video/2005_Defcon_V2-P_Zimmerman-Unveiling_My_Next_Big_Project.mp4 HTTP/1.1
Host: media.defcon.org
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.0.8) Gecko/2009032608 Firefox/3.0.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.defcon.org/html/links/defcon-media-archives.html
Pragma: no-cache
Cache-Control: no-cache
HTTP/1.x 200 OK
Server: lighttpd
Date: Sun, 05 Apr 2009 05:20:12 GMT
Last-Modified: Tue, 23 May 2006 22:18:19 GMT
Content-Type: video/mp4
Transfer-Encoding: chunked
Update:
Is there a header that I can send that will tell Lighthttpd not to use chunked encoding.My original problem is that I am using urlConnection
to grab the file in my java application which automatically sends HTTP 1.1 request.
I would like to know the size of the file so i can update my percentage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Firefox 正在执行 HTTP 1.1 GET 请求。 Lighthttpd 知道客户端将支持分块传输编码并以块的形式返回内容,每个块报告其自己的长度。
另一方面,Wget 执行 HTTP 1.0 GET 请求。 Lighthttpd 了解客户端不支持 HTTP 1.1(因此不支持分块传输编码),因此会以单个块的形式返回内容,并在响应标头中报告长度。
Firefox is performing an HTTP 1.1 GET request. Lighthttpd understands that the client will support chunked-transfer encoding and returns the content in chunks, with each chunk reporting its own length.
Wget on the other hand performs an HTTP 1.0 GET request. Lighthttpd, understanding that the client doesn't support HTTP 1.1 (and thus chunked-transfer encoding), returns the content in one single chunk, with the length reported in the response header.
看起来是因为
分块
传输编码:这会将视频分块发送,每个块都有自己的大小。 这是在 HTTP 1.1 中定义的,Firefox 使用的是 HTTP 1.1,而 wget 使用的是 HTTP 1.0,它不支持分块传输编码,因此服务器必须立即发送整个文件。
Looks like it's because of the
chunked
transfer encoding:This will send the video down in chunks, each with its own size. This is defined in HTTP 1.1, which is what Firefox is using, while
wget
is using HTTP 1.0, which doesn't support chunked transfer encoding, so the server has to send the whole file at once.我遇到了同样的问题,并找到了解决方案,无论哪个 HTTP 版本:
HEAD
向服务器发出请求,该请求仅使用 HTTP 标头而没有内容正确响应。 此标头正确包含要下载的文件所需的Content-Length:
字节大小。GET
请求下载文件(GET 响应的标头无法包含 Content-length)。Objective-C 语言示例:
以及输出:(
注意:此示例 URL 确实正确提供了 GET 响应中的标头 Content-Length,但如果未能提供,则显示想法)
I was having the same problem and found a solution regardless of which HTTP version:
HEAD
request to the server which correctly responds with just the HTTP header and no contents. This header correctly includes the wantedContent-Length:
bytes size for the file to download.GET
request to download the file (the header from the GET response fails to include Content-length).An Objective-C language example:
And the output:
(Note: This example URL does correctly provides the header Content-Length from the GET response, but shows the idea if it failed to)