application / * Content-Type和字符集属性 [英] application/* Content-Type and charset attributes
问题描述
3.7.1中的 RFC-2616 状态:
The RFC-2616 states in 3.7.1:
当发件人未提供显式字符集参数时,文本类型的媒体
子类型定义为具有默认字符集
通过HTTP接收到的 ISO-8859-1值。
When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.
这就是为什么我通常使用例如文本/纯文本; charset = utf-8
作为 Content-Type
标头。
This is why I usually use e.g. text/plain; charset=utf-8
as Content-Type
Header.
MediaTypes of输入应用
What about MediaTypes of type application
?
我经常看到und use标头,例如 Content-Type:application / xml; charset = UTF-8
。然后,RESTeasy 2.3.7会强制客户端也在 Accept
标头中发送charset参数。否则,它将回答 406
。 RESTeasy 3.0.6在这里似乎更为实用,所以我不确定这里的最佳实践是什么。
I often see und use headers like Content-Type: application/xml; charset=UTF-8
. RESTeasy 2.3.7 then forces the client to also send the charset parameter in the Accept
header. Otherwise it will answer with a 406
. RESTeasy 3.0.6 seems to be quite more tolearant here so I'm unsure what's the best practice here.
推荐答案
RFC 2616是2014年6月被一组RFC淘汰,其中包含通用HTTP规范的RFC是 RFC 7213 。请使用 RFC编辑器检查RFC的当前状态。
RFC 2616 was obsoleted in June 2014 by a set of RFCs, where the one containing the general HTTP specifications is RFC 7213. Please use the RFC editor to check the current status of RFCs.
RFC 7213明确表示(在附录B中):
RFC 7213 explicitly says (in Appendix B):
ISO-8859-1文本媒体类型的默认字符集已被
删除;
The default charset of ISO-8859-1 for text media types has been
removed; the default is now whatever the media type definition says.
另一方面, RFC 6657 在预期此类更改的同时,声明:
On the other hand, RFC 6657, while anticipating such changes, declares:
文本/纯文本的默认字符集参数值从[RFC2046]起未更改
,并保持为 US-ASCII。
The default "charset" parameter value for "text/plain" is unchanged from [RFC2046] and remains as "US-ASCII".
因此,如果您的数据不是ASCII(= US-ASCII),则应继续显式声明 charset
参数。
Thus, if your data is not ASCII (= US-ASCII), you should keep declaring the charset
parameter explicitly.
XML规范的子句 4.3.3 ,指定:
The XML specification, clause 4.3.3, specifies:
在没有外部字符编码信息(例如
MIME标头)的情况下,解析的实体存储在编码除UTF-8或UTF-16之外的其他
必须以包含编码声明的文本声明[...]
开头
In the absence of external character encoding information (such as MIME headers), parsed entities which are stored in an encoding other than UTF-8 or UTF-16 MUST begin with a text declaration [...] containing an encoding declaration
因此,对于通过HTTP传输的XML,无论内容类型如何,都必须在HTTP标头或编码声明中明确设置编码,例如<?xml encoding ='UTF-8'?>
。
So for XML transmitted over HTTP, irrespective of content type, the encoding MUST be explicitly set either in an HTTP header or in an encoding declaration, e.g. <?xml encoding='UTF-8'?>
.
对于一般情况下,可能会应用类型特定的规则。字符编码与大多数
application
类型无关,因为这些类型定义了自己的编码方案,包括任何嵌入式字符数据的编码。
For application
types in general, type-specific rules may apply. Character encoding is irrelevant to most application
types, as the types define their own encoding schemes, including the encoding of any embedded character data.
这篇关于application / * Content-Type和字符集属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!