Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 369
previous_developer Avatar asked Jul 07 '12 13:07

previous_developer


People also ask

Is Content Length mandatory for a HTTP response?

The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.

Is Content Length header required in POST request?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.

Can HTTP response messages be empty?

False. e) HTTP response messages never have an empty message body.

What is Content Length in HTTP response?

The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.


1 Answers

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

like image 142
Julian Reschke Avatar answered Sep 26 '22 02:09

Julian Reschke