为 GET 请求设置的正确内容长度是多少?
当我使用以下代码发出 POST
请求时:
string body = "Hello World";
byte[] bytes = Encoding.ASCII.GetBytes(body);
WebRequest request = WebRequest.Create("http://internalurl");
request.Method = "POST";
request.ContentLength = bytes.Length;
我将内容长度设置为 POST
ed 的字节数。 GET
请求的正确 ContentLength
是多少?
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 POST
ed.
What is the correct ContentLength
for a GET
request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于在执行
GET
请求时通常不会发送任何其他数据,因此根本不应发送标头Content-Length
。仅当您发送消息正文时才应包含标头
Content-Length
,并且相关标头的值始终是该字段的长度,以单位为单位测量(OCTET) 字节。(据我所知)在执行
GET
请求时包含 message-body 被认为是不好的做法,但在读取 HTTP RFC2616 我没有看到任何说明GET
请求不能包含消息正文。不过,如果您在消息正文中发送数据并希望在这种情况下对其进行解析和处理,那么今天的大多数网络服务器都不会回复您希望它们回复的内容。
Since you normally doesn't send any additional data when you do a
GET
request, the headerContent-Length
should not be sent at all.The header
Content-Length
should 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
GET
request, but when reading the HTTP RFC2616 I see nothing stating that aGET
request 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.