如何使用 HTML 链接在请求标头中设置“Content-Type ... charset”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6228960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:55:21  来源:igfitidea点击:

How to set the "Content-Type ... charset" in the request header using a HTML link

htmlcharacter-encodingspecial-charactersurlencode

提问by HAL 9000

I have a simple HTML-page with a UTF-8 encoded link.

我有一个带有 UTF-8 编码链接的简单 HTML 页面。

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <a charset='UTF-8' href='http://server/search?q=%C3%BC'>search for "ü"</a>
  </body>
</html>

However, I don't get the browser to include Content-Type:application/x-www-form-urlencoded; charset=utf-8into the request header. Therefore I have to configure the webserver to assume all requests are UTF-8 encoded (URIEncoding="UTF-8" in Tomcat server.xml). But of course the admin won't let me do that in the production environment (Websphere).

但是,我没有让浏览器包含Content-Type:application/x-www-form-urlencoded; charset=utf-8在请求标头中。因此,我必须配置网络服务器以假设所有请求都是 UTF-8 编码的(Tomcat server.xml 中的 URIEncoding="UTF-8")。但当然管理员不会让我在生产环境(Websphere)中这样做。

I know it's quite easy to achieve using Ajax, but how can I control the request header when using standard HTML links? The charsetattribute doesn't seem to work for me (tested in IE8 and FF 3.5)

我知道使用 Ajax 很容易实现,但是在使用标准 HTML 链接时如何控制请求标头?该charset属性似乎对我不起作用(在 IE8 和 FF 3.5 中测试)

The 2nd part of the required solution would be to set the URL encoding when changing an IFrame's document.locationusing Javascript.

所需解决方案的第二部分是在document.location使用 Javascript更改 IFrame 时设置 URL 编码。

回答by BalusC

This is not possible from HTML on. The closest what you can get is the accept-charsetattribute of the <form>. Only MSIE browser adheres that, but even then it is doing it wrong (e.g. CP1252 is actuallybeen used when it says that it has sent ISO-8859-1). Other browsers are fully ignoring it and they are using the charset as specified in the Content-Typeheader of the response. Setting the character encoding right is basically fully the responsiblity of the server side. The client side should just send it back in the same charset as the server has sent the response in.

从 HTML 开始,这是不可能的。你能得到的最接近accept-charset的是<form>. 只有 MSIE 浏览器坚持这一点,但即便如此,它还是做错了(例如,当它说它已发送 ISO-8859-1 时,实际上使用了CP1252 )。其他浏览器完全忽略它,他们使用Content-Type响应标头中指定的字符集。设置正确的字符编码基本上完全由服务器端负责。客户端应该只将它以与服务器发送响应相同的字符集发送回来。

To the point, you should really configure the character encoding stuff entirely from the server side on. To overcome the inability to edit URIEncodingattribute, someone here on SO wrote a (complex) filter: Detect the URI encoding automatically in Tomcat. You may find it useful as well (note: I haven't tested it).

就此而言,您应该完全从服务器端配置字符编码。为了克服无法编辑URIEncoding属性的问题,这里有人写了一个(复杂的)过滤器:在 Tomcat 中自动检测 URI 编码。您可能会发现它也很有用(注意:我还没有测试过)。



Update: Noted should be that the meta tag as given in your question is ignoredwhen the content is been transferred over HTTP. Instead, the HTTP response Content-Typeheader will be used to determine the content type and character encoding. You can determine the HTTP header with for example Firebug, in the Netpanel.

更新:应该注意的是,当通过 HTTP 传输内容时,您的问题中给出的元标记将被忽略。相反,HTTP 响应Content-Type头将用于确定内容类型和字符编码。例如,您可以在Net面板中使用Firebug确定 HTTP 标头。

alt text

替代文字