gpt4 book ai didi

c# - HTTP HEAD 响应 - 设置 Content-Length

转载 作者:行者123 更新时间:2023-11-30 17:24:13 24 4
gpt4 key购买 nike

我正在尝试为我们的一项服务构建 HEAD 方法,以便客户可以在决定是否下载之前查看内容类型和大小。

如何设置响应头的内容长度?使用 HttpContext 会公开 ContentType、Encoding 等,但我无法指定长度,因为我认为在将某些内容添加到响应时框架通常会添加此值。

是否有另一种方法可以做到这一点,或者我为 HEAD 请求设置长度是否不正确 - 我实际上应该添加自定义 header 以返回资源的大小吗?

最佳答案

我认为 Content-Length 指定响应正文的大小 - 而不是资源的大小。因此,它在 HEAD 响应的上下文中没有意义。

编辑:规范说:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

-- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

这表明我上面的说法是错误的。特别是后一段强烈建议 HEAD 可以有一个 Content-Length header 。

关于c# - HTTP HEAD 响应 - 设置 Content-Length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1311892/

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