简体   繁体   English

是否始终需要Content-Type HTTP标头?

[英]Is Content-Type HTTP header always required?

This question is about browser behavior as well as protocol specification for linking, importing, including or ajaxing css, js, image and other resources from within html, js or css files. 这个问题是关于浏览器行为以及从html,js或css文件中链接,导入,包含或者调用css,js,image和其他资源的协议规范。

While testing static files and compressed content delivery in different browsers, I found that some browsers start behaving differently if you move away from conventions. 在不同的浏览器中测试静态文件和压缩内容传递时,我发现如果您偏离约定,某些浏览器的行为会有所不同。 For example, IE6 creates problem if you do not send Content-Disposition: inline; 例如,如果您不发送Content-Disposition: inline; ,则IE6会产生问题Content-Disposition: inline; header for all inline css and js etc files, and a recent version of safari does not properly handle pre-compressed gzip CSS files if you use file extension .gz like in main-styles.css.gz . 所有内联css和js等文件的标题,如果你在main-styles.css.gz使用文件扩展名.gz则最新版本的safari无法正确处理预压缩的gzip CSS文件。

My question is about the behavior of browsers about Content-Type response header. 我的问题是浏览器关于Content-Type响应头的行为。 Since <link> , <script> and <img> tags already reasonably specify the content type of the resource, can this header be safely skipped, or do some browsers require it for some historical reason? 由于<link><script><img>标签已经合理地指定了资源的内容类型,是否可以安全地跳过此标头,或者某些浏览器是否因某些历史原因需要它?

In short, no, it's not required. 简而言之,不,这不是必需的。 But it's recommended. 但它是推荐的。 Most browser that I know of will treat <link> , <script> , and <img> properly if they are not sent with headers, but there's no real good reason not to send the headers. 我知道的大多数浏览器如果没有带头文件,会正确处理<link><script><img> ,但没有真正的理由不发送头文件。 Basically, without Content-Type headers, the browser is left to try and guess based on the content. 基本上,如果没有Content-Type标头,浏览器就会根据内容尝试猜测。

From RFC2616: 来自RFC2616:

Content-Type specifies the media type of the underlying data. Content-Type指定基础数据的媒体类型。
Content-Encoding may be used to indicate any additional content 内容编码可用于指示任何附加内容
codings applied to the data, usually for the purpose of data 编码应用于数据,通常用于数据目的
compression, that are a property of the requested resource. 压缩,它是所请求资源的属性。 There is
no default encoding. 没有默认编码。

Any HTTP/1.1 message containing an entity-body SHOULD include a 任何包含实体主体的HTTP / 1.1消息都应该包含一个
Content-Type header field defining the media type of that body. Content-Type标头字段,用于定义该主体的媒体类型。 If 如果
and only if the media type is not given by a Content-Type field, the 并且只有当Content-Type字段没有给出媒体类型时,
recipient MAY attempt to guess the media type via inspection of its 收件人可以尝试通过检查媒体类型来猜测媒体类型
content and/or the name extension(s) of the URI used to identify the 用于标识的URI的内容和/或名称扩展名
resource. 资源。 If the media type remains unknown, the recipient SHOULD 如果媒体类型仍然未知,则收件人应该
treat it as type "application/octet-stream". 将其视为“application / octet-stream”类型。

Regarding the keyword SHOULD, specified in RFC2119: 关于RFC2119中指定的关键字SHOULD:

SHOULD: This word, or the adjective "RECOMMENDED", mean that there 应该:这个词,或形容词“推荐”,意思是那里
may exist valid reasons in particular circumstances to ignore a 在特定情况下可能存在有效理由忽略a
particular item, but the full implications must be understood and 特殊项目,但必须理解全部含义
carefully weighed before choosing a different course. 在选择不同的课程之前仔细权衡。

I ran into a problem in java where I tried to post some data via the library chrriis.dj.nativeswing.swtimpl.components.JWebBrowser, which basically displays an internet explorer inside a java program. 我在java中遇到了一个问题,我试图通过库chrriis.dj.nativeswing.swtimpl.components.JWebBrowser发布一些数据,它基本上在java程序中显示一个Internet Explorer。 But the simple php script on the back-end would not parse my post-data. 但是后端的简单php脚本不会解析我的后期数据。 (Used WebBrowserNavigationParameters to set post data while navigating to a certain page) I finally found out that the Content-Type header had to be set for php to properly paste the post-data. (使用WebBrowserNavigationParameters在导航到某个页面时设置发布数据)我终于发现必须为php设置Content-Type标头以正确粘贴后期数据。 (This was not set by default.) Setting it to Content-Type: application/x-www-form-urlencoded and everything worked fine. (默认情况下没有设置。)将其设置为Content-Type:application / x-www-form-urlencoded,一切正常。 So, I guess setting Content-Type should allways be done when POSTing data to php. 所以,我想在将数据发布到php时,总是应该设置Content-Type。

It is required for backward compatibility. 向后兼容性是必需的。

For example: Internet Explorer 10 needs Content-Type:image/svg+xml in order to render any svg file 例如: Internet Explorer 10需要Content-Type:image/svg+xml才能呈现任何svg文件

IE10 , IE9 and probably other browsers always need the Content-Type header. IE10IE9和其他浏览器总是需要Content-Type标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM