Accept 和 Accept-Charset - 哪个更好? [英] Accept and Accept-Charset - Which is superior?

查看:57
本文介绍了Accept 和 Accept-Charset - 哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 HTTP 中,您可以在请求中指定您的客户端可以使用 accept 标头接受响应中的特定内容,其值例如 application/xml.内容类型规范允许您在内容类型中包含参数,例如charset=utf-8,表示您可以接受具有指定字符集的内容.

In HTTP you can specify in a request that your client can accept specific content in responses using the accept header, with values such as application/xml. The content type specification allows you to include parameters in the content type, such as charset=utf-8, indicating that you can accept content with a specified character set.

还有accept-charset头,指定客户端接受的字符编码.

There is also the accept-charset header, which specifies the character encodings which are accepted by the client.

如果两个头都被指定,并且 accept 头包含带有 charset 参数的内容类型,那么服务器应该将其视为上级头?

If both headers are specified and the accept header contains content types with the charset parameter, which should be considered the superior header by the server?

例如:

Accept: application/xml; q=1,
        text/plain; charset=ISO-8859-1; q=0.8
Accept-Charset: UTF-8

我使用 Fiddler 向各种服务器发送了一些示例请求,以测试它们的响应方式:

I've sent a few example requests to various servers using Fiddler to test how they respond:

示例

W3

请求

GET http://www.w3.org/ HTTP/1.1
Host: www.w3.org
Accept: text/html;charset=UTF-8
Accept-Charset: ISO-8859-1

回应

Content-Type: text/html; charset=utf-8

谷歌

请求

GET http://www.google.co.uk/ HTTP/1.1
Host: www.google.co.uk
Accept: text/html;charset=UTF-8
Accept-Charset: ISO-8859-1

回应

Content-Type: text/html; charset=ISO-8859-1

堆栈溢出

请求

GET http://stackoverflow.com/ HTTP/1.1
Host: stackoverflow.com
Accept: text/html;charset=UTF-8
Accept-Charset: ISO-8859-1

回应

Content-Type: text/html; charset=utf-8

微软

请求

GET http://www.microsoft.com/ HTTP/1.1
Host: www.microsoft.com
Accept: text/html;charset=UTF-8
Accept-Charset: ISO-8859-1

回应

Content-Type: text/html

对于预期的行为似乎没有任何共识.我试着装出惊讶的样子.

There doesn't seem to be any consensus around what the expected behaviour is. I am trying to look surprised.

推荐答案

虽然您可以在 Accept 标头中设置媒体类型,但该媒体类型的 charset 参数定义是未在 RFC 2616 中的任何地方定义(但并不禁止).

Altough you can set media type in Accept header, the charset parameter definition for that media type is not defined anywhere in RFC 2616 (but it is not forbidden, though).

因此,如果你要实现一个符合 HTTP 1.1 的服务器,你应该首先寻找 Accept-charset 头,然后在 Accept 头中搜索你自己的参数.

Therefore if you are going to implement a HTTP 1.1 compliant server, you shall first look for Accept-charset header, and then search for your own parameters at Accept header.

这篇关于Accept 和 Accept-Charset - 哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆