When I make a POST request using the following code:
string body = "Hello World";
byte[] bytes = Encoding.ASCII.GetBytes(body);
WebRequest request = WebRequest.Create("http://internalurl");
request.Method = "POST";
request.ContentLength = bytes.Length;
I set the content length to the number of bytes POSTed.
What is the correct ContentLength for a GET request?
Since you normally doesn’t send any additional data when you do a
GETrequest, the headerContent-Lengthshould not be sent at all.The header
Content-Lengthshould only be included when you are sending a message-body, and the value of the header in question is always the length of this field, measured in (OCTETs) bytes.It’s (AFAIK) considered bad practice to include a message-body when doing a
GETrequest, but when reading the HTTP RFC2616 I see nothing stating that aGETrequest cannot include a message-body.Though I will assume that most web servers today will not reply with what you want them to reply if you send data in a message-body and expects it to be parsed and handled in that case.