Search code examples
httptcphttp-headers

Getting response of http request without content-length?


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?


Solution

  • In HTTP/1.0 - server response without content-length is when the stream closes

    In HTTP/1.1 - server response without content-length is when the response is chunked encoded