为GET请求设置的正确Content-Length是什么? [英] What is the correct Content-Length to set for a GET request?

查看:5979
本文介绍了为GET请求设置的正确Content-Length是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下代码进行 POST 请求时:

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;

我将内容长度设置为字节数 POST 编辑。
GET 请求的正确 ContentLength 是什么?

I set the content length to the number of bytes POSTed. What is the correct ContentLength for a GET request?

推荐答案

由于您在执行 GET 请求时通常不会发送任何其他数据,因此标题<$ c不应该发送$ c> Content-Length 。

Since you normally doesn't send any additional data when you do a GET request, the header Content-Length should not be sent at all.

只有在发送 message-body时才应包含标题 Content-Length em>,并且有问题的标头的值始终是此字段的长度,以( OCTETs )字节为单位。

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.


(RFC2616)14.13内容长度



Content-Length实体标题字段指示实体主体的大小,以十进制数字表示的OCTET,发送给收件人,或者在HEAD方法的情况下,大小如果请求是GET,则本机身体将被发送。

(RFC2616) 14.13 Content-Length

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

< snip />

<snip />

应用程序应该使用此字段来指示消息正文的传输长度,除非 4.4

Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.






这是(AFA IK)在执行 GET 请求时,考虑了不良做法,包括 message-body ,但在读取HTTP RFC2616 我什么也没看到 GET 请求不能包含 message-body


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 a GET request cannot include a message-body.

虽然我会假设今天大多数网络服务器都不会回复您希望他们回复的内容在 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.


(RFC2616)4.3消息正文



HTTP消息的消息体(如果有)用于携带
实体 - 与请求或响应相关联的主体。仅当传输编码已经应用
时,message-body
与entity-body不同,如Transfer-Encoding头字段所示(
14.41)。

(RFC2616) 4.3 Message Body

The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response. The message-body differs from the entity-body only when a transfer-coding has been applied, as indicated by the Transfer-Encoding header field (section 14.41).

   message-body = entity-body
                | <entity-body encoded as per Transfer-Encoding>

转移编码必须用于指示应用程序应用的任何转移编码
确保安全和正确地传输
消息。 Transfer-Encoding是消息的属性,而不是实体的属性,因此可以由
请求/响应链中的任何应用程序添加或删除。 (但是,当可以使用某些转移编码时,第3.6节对
进行限制。)

Transfer-Encoding MUST be used to indicate any transfer-codings applied by an application to ensure safe and proper transfer of the message. Transfer-Encoding is a property of the message, not of the entity, and thus MAY be added or removed by any application along the request/response chain. (However, section 3.6 places restrictions on when certain transfer-codings may be used.)

消息允许消息正文的规则
请求和响应不同。

The rules for when a message-body is allowed in a message differ for requests and responses.

请求中消息正文的存在由
包含Content-Length或
请求的消息头中的Transfer-Encoding头字段。

The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers.

如果请求方法的规范,则消息体不得包含在请求中(第5.1.1节)不允许在请求中发送实体主体。

A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests.

服务器应该在任何请求上读取和转发消息体;如果请求方法
不包含实体主体的定义语义,那么在处理请求时应该忽略
消息体。

A server SHOULD read and forward a message-body on any request; if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

这篇关于为GET请求设置的正确Content-Length是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆