How to know when HTTP-server is done sending data

别来无恙 提交于 2019-12-22 10:58:16

问题


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.


回答1:


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:

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

source Wikipedia




回答2:


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 the Content-length is set or not. Performance will be partially affected by this




回答3:


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

 int stream.read(byte[],size);

returns the real size of byte[]-argument's size that has been read till socket-close or socket-timeout (or size-argument reached).

Regards.



来源:https://stackoverflow.com/questions/9412945/how-to-know-when-http-server-is-done-sending-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!