Getting response of http request without content-l

2019-03-14 23:12发布

问题:

I have a little program sends http request and gets response with TCP protocol.

My request format;

GET / HTTP/1.0
Host: somewebsite.com
{two new line}

I read response line by line from socket (using NetworkStream and StreamReader in c#) until I find content-length header. I store the length, then continue reading until find an empty line. Then create a buffer with the length and receive the rest of response.

But some reponses does not have a content-length header. So my approach fails. If I don't know how many bytes I should receive, when I should stop?

回答1:

In HTTP/1.0? When the stream closes.

In HTTP/1.1? With chunked encoding.



回答2:

See relevant part of HTTP spec. In your specific case, if the server does not give content length back, then it MUST be closing the stream upon finishing the response. There is no other reliable way for you (as a client) to know. Regardless of HTTP version. @Julian chunked encoding is indeed a clever upgrade in HTTP/1.1 but is rather specific to streaming and there is no reason why a "plain" webserver would implement it. That is a server which knows the content length before initiating response. And i guess that the OP doesn't have the server under control, otherwise he won't be objecting missing HTTP headers.

But even if you DO get the content length header, you must not unreservedly trust it. The server implementors are only fallible humans too. Take it as a "most probable" response, initial value to a resizable buffer. You still must be ready to handle less as well as more (the worse case).