简体   繁体   English

要求客户端在http content-type标头字段中指定字符集有什么问题吗?

[英]Is there anything wrong with requiring the client to specify the charset in the http content-type header field?

I'm implementing a service (as rest) that receives a POST method. 我正在实现一个接收POST方法的服务(其余)。

The encoding in my sistem is UTF-8. 我系统中的编码是UTF-8。

I'm using jboss 5, in which the servlet that receives the requests follows the HTTP 1.1 specification of rfc2068 which states that: 我正在使用jboss 5,其中接收请求的servlet遵循rfc2068HTTP 1.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. 当发送方未提供任何明确的字符集参数时,“文本”类型的媒体子类型将定义为在通过HTTP接收时具有默认的字符集值ISO-8859-1 ”。

so when the client that invokes my service is using for example UTF-8 and doesn't specify a charset, and the body of the POST contains characters outside the US-ASCII, the Jboss servlet assumes " ISO-8859-1 " and does a "wrong" decodification and in my system i receive "broken" characters. 因此,当调用我的服务的客户端使用例如UTF-8且未指定字符集,并且POST的正文包含US-ASCII之外的字符时,Jboss servlet假定为“ ISO-8859-1 ”并执行“错误的”编解码,在我的系统中我收到“损坏的”字符。 For example instead of the string "día" i receive "dÂa". 例如,我收到的不是字符串“día”,而是“dÂa”。

The approach i found for "protecting" my system is to require the client to specify the charset in the content-type parameter. 我发现的用于“保护”系统的方法是要求客户端在content-type参数中指定字符集 If a charset is not specified then i respond with an http 403 and a text indicating that "the charset value must be specified". 如果未指定字符集,那么我将使用http 403和指示“必须指定字符集值”的文本进行响应。

Is there anything wrong with this approach? 这种方法有什么问题吗?

RFC 2068 has been obsoleted twice and really is irrelevant. RFC 2068已被废弃两次,实际上是无关紧要的。 You need to look at RFC 7231, which doesn't define a default anymore. 您需要查看RFC 7231,它不再定义默认值。 This means that the default is governed by the definition of the media type. 这意味着默认值由媒体类型的定义控制。

For text/plain, this implies US-ASCII (as far as I remember), so clients that want to send non-ASCII characters really need to specify the charset. 对于文本/纯文本,这暗示着US-ASCII(据我所记得),因此想要发送非ASCII字符的客户端确实需要指定字符集。

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

相关问题 HTTP Content-Type响应中的错误字符集 - Wrong charset in HTTP Content-Type response Content-Type HTTP标头中“charset”的范围 - Scope of “charset” in Content-Type HTTP header 在golang HTTP FileServer的Content-Type标头上设置'charset'属性 - Setting the 'charset' property on the Content-Type header in the golang HTTP FileServer 字符集组件在HTTP内容类型标头中是否是必需的? - Is the charset component mandatory in the HTTP content-type header? 为非文本媒体类型发送带有Content-Type标头的charset参数是绝对错误的吗? - Is it categorically wrong to send a charset parameter with a Content-Type header for non-text media types? application / pdf content-type http响应头的正确charset是什么? - what is the correct charset for a application/pdf content-type http response header? Java应用程序服务器如何决定在HTTP Content-type标头中发送什么字符集? - How does a Java application server decide what charset to send in the HTTP Content-type header? API Get 需要 Content-Type application/json;charset=UTF-8 - Http 客户端问题 - API Get requires Content-Type application/json;charset=UTF-8 - Issue with Http Client 是内容类型“text/xml; 字符集=utf-8”错了吗? - Is content-type “text/xml; charset=utf-8” wrong? 是否始终需要Content-Type HTTP标头? - Is Content-Type HTTP header always required?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM