如何知道什么时候HTTP服务器完成发送数据 [英] How to know when HTTP-server is done sending data

查看:502
本文介绍了如何知道什么时候HTTP服务器完成发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个浏览器/代理为本项目中,我需要下载网页。发送自定义HTTP请求到Web服务器我开始监听服务器响应之后。

I'm working on a browser/proxy oriented project where I need to download webpages. After sending a custom HTTP request to a web server I start listening for a server response.

在读取响应,我检查响应头的内容长度:-ROW。如果我得到其中的一个,很容易确定当服务器完成了数据传送,因为我一直知道有多少字节的数据我已经收到了。

When reading the response, I check the response headers for a Content-Length:-row. If I get one of those, it's easy to determine when the server is done sending data since I always know how many bytes of data I have received.

当服务器不包含Content-Length头,并保持开放的进一步请求的连接出现问题。例如,谷歌服务器gzip压缩内容作出响应,但不包括内容的长度。我怎么知道什么时候停止等待更多的数据,并关闭连接?

The problem occurs when the server doesn't include the Content-Length header and also keeps the connection open for further requests. For example, the google server responds with gzipped-content, but doesn't include content length. How do I know when to stop waiting for more data and close the connection?

我已经考虑过使用的超时值时,没有数据已收到了一会儿就关闭连接,但是这似乎是错误的方式做到这一点。 Chrome浏览器,例如,可以下载相同的页面作为我,似乎总是知道什么时候关闭连接。

I have considered using a timeout value to close the connection when no data has been received for a while, but this seems like the wrong way to do it. Chrome for example, can download the same pages as me and always seem to know exactly when to close the connection.

推荐答案

有一个在IETF RFC 2616,搜索块编码和内容范围。

Have a look at IETF RfC 2616, search for chunked encoding and Content-Range.

HTTP用于返回未知长度的内容,如:

HTTP is designed to return content of unknown length, as in:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

源维基

这篇关于如何知道什么时候HTTP服务器完成发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆