如何知道 HTTP 服务器何时完成发送数据
我正在开发一个面向浏览器/代理的项目,我需要下载网页。向 Web 服务器发送自定义 HTTP 请求后,我开始监听服务器响应。
读取响应时,我检查响应标头中的 Content-Length:-row。如果我得到其中之一,很容易确定服务器何时完成发送数据,因为我总是知道我收到了多少字节的数据。
当服务器不包含 Content-Length 标头并且还保持连接打开以接受进一步请求时,就会出现此问题。例如,谷歌服务器响应 gzipped-content,但不包括内容长度。我如何知道何时停止等待更多数据并关闭连接?
我曾考虑过在一段时间内没有收到数据时使用超时值来关闭连接,但这似乎是错误的方法。例如,Chrome 可以下载与我相同的页面,并且似乎总是确切地知道何时关闭连接。
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.
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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 IETF RfC 2616,搜索分块编码和 Content-Range。
HTTP 旨在返回未知长度的内容,如:
源维基百科
Have a look at IETF RfC 2616, search for chunked encoding and Content-Range.
HTTP is designed to return content of unknown length, as in:
source Wikipedia
我会尝试建议您强制
Connection: close
标头,以便确保服务器在输出完成后关闭连接,无论是否设置了Content-length
或不。性能将受到部分影响I would try to suggest you to force
Connection: close
header so you are sure that the server closes the connection after output is finished, no matter if theContent-length
is set or not. Performance will be partially affected by this您可能会遇到两种情况:
1. 套接字关闭
2.socket-timeout
通常socket会被关闭,声明一个Socket Timeout也是有意义的。
请记住
返回直到套接字关闭或套接字超时(或达到大小参数)为止已读取的字节[]参数大小的实际大小。
问候。
There are two cases you can expect:
1. socket-close
2. socket-timeout
Usually the socket will be closed, it also make sense to declare an Socket Timeout.
Remember
returns the real size of byte[]-argument's size that has been read till socket-close or socket-timeout (or size-argument reached).
Regards.