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?
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.
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.
False. e) HTTP response messages never have an empty message body.
The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With