为 GET 请求设置的正确内容长度是多少?

发布于 2024-12-21 13:09:23 字数 365 浏览 2 评论 0原文

当我使用以下代码发出 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 是多少?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-12-28 13:09:23

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

仅当您发送消息正文时才应包含标头 Content-Length,并且相关标头的值始终是该字段的长度,以单位为单位测量(OCTET) 字节。

(RFC2616) 14.13 内容长度

Content-Length 实体标头字段指示发送给接收者的实体主体的大小(以十进制数的八位字节数表示),或者在 HEAD 方法的情况下,指示实体主体的大小如果请求是 GET,则已发送。

<截图/>

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


(据我所知)在执行 GET 请求时包含 message-body 被认为是不好的做法,但在读取 HTTP RFC2616 我没有看到任何说明 GET 请求不能包含消息正文

不过,如果您在消息正文中发送数据并希望在这种情况下对其进行解析和处理,那么今天的大多数网络服务器都不会回复您希望它们回复的内容。

(RFC2616) 4.3 消息正文

HTTP 消息的消息体(如果有)用于携带
与请求或响应关联的实体主体。消息正文
仅当传输编码已完成时才与实体主体不同
应用,如传输编码头字段所示(部分
14.41)。

 消息正文 = 实体正文
                | <按照传输编码编码的实体主体>

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

消息中允许使用消息正文的规则因情况而异
请求和响应。

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

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

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

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.

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

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.


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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文