gpt4 book ai didi

http - 为 GET 请求设置的正确 Content-Length 是多少?

转载 作者:可可西里 更新时间:2023-11-01 15:07:09 26 4
gpt4 key购买 nike

当我使用以下代码发出 POST 请求时:

string body = "Hello World";
byte[] bytes = Encoding.ASCII.GetBytes(body);
WebRequest request = WebRequest.Create("http://internalurl");
request.Method = "POST";
request.ContentLength = bytes.Length;

我将内容长度设置为 POSTed 的字节数。GET 请求的正确 ContentLength 是多少?

最佳答案

由于您在执行 GET 请求时通常不会发送任何其他数据,因此根本不应发送 header Content-Length

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

(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 />

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.


在执行 GET 请求时包含 message-body 被认为是不好的做法,但在读取 HTTP RFC2616 时我没有看到任何说明 GET 请求不能包含 message-body 的内容。

虽然我假设如果您在 message-body 中发送数据并希望在这种情况下对其进行解析和处理,那么今天的大多数网络服务器不会回复您希望它们回复的内容。

(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 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.

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.

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.

关于http - 为 GET 请求设置的正确 Content-Length 是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8540931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com