Safari 根据请求对已编码的 URL 进行编码
我在 Safari 中使用以下 URL 对页面执行 HTTP GET 请求:mysite.com/page.aspx?param=v%e5r
该页面包含一个可以回发给自身的表单。 asp.net 输出时的 HTML 表单标签如下所示:
当 Safari 将此 URL 发回时,它会以某种方式将此 URL 转换为:page.aspx?param=v%25u00e5r
,即对已经URL编码的字符串进行URL编码,然后进行双重编码,该参数生成的输出是乱码(vå r )。在打印参数之前,我可以通过对参数进行 URL 解码来解决这个问题。
Firefox 甚至 IE8 都能很好地处理这个问题。 这是 WebKit 中的错误还是我做错了什么?
总结一下:
mysite.com/page.aspx?param=v%e5r
HTML:
mysite.com/page.aspx?param=v%25u00e5r
HTML:
I do an HTTP GET request for a page using the following URL in Safari:mysite.com/page.aspx?param=v%e5r
The page contains a form which posts back to itself.
The HTML form tag looks like this when output by asp.net:<form method="post" action="page.aspx?param=v%u00e5r" id="aspnetForm" >
When Safari POSTs this back it somehow converts this URL to:page.aspx?param=v%25u00e5r
, i.e. it URL encodes the already URL encoded string, which is then double encoded and the output generated by this parameter is garbled (vår
). I am able to get around this some places by URL decoding the parameter before printing it.
Firefox and even IE8 handles this fine. Is this a bug in WebKit or am I doing something wrong?
To summarise:
mysite.com/page.aspx?param=v%e5r
HTML:
<form method="post" action="page.aspx?param=v%u00e5r" id="aspnetForm" >
mysite.com/page.aspx?param=v%25u00e5r
HTML:
<form method="post" action="page.aspx?param=v%25u00e5r" id="aspnetForm" >
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然您可以在 URL 的查询部分中使用 UTF-8 以外的编码,但这是不可取的,并且通常会混淆各种采用 UTF-8 的脚本。
您确实希望在标记为 UTF-8 的页面中生成表单,然后在应用程序中接受 UTF-8 并将字符串
vår
(假设这就是您的意思)编码为param=v %C3%A5r
。哦亲爱的!这是非常错误的。
%uXXXX
仅是 JavaScript-escape()
样式的序列;输入 URL 是完全无效的。 Safari 可能试图通过对后面没有带有%25
的两位十六进制序列的%
进行编码来修复错误。这是 ASP.NET 生成的吗?如果是这样,那就太令人失望了。您如何创建
Whilst you can use encodings other than UTF-8 in the query part of a URL, it's inadvisable and will generally confuse a variety of scripts that assume UTF-8.
You really want to be producing forms in pages marked as being UTF-8, then accepting UTF-8 in your application and encoding the string
vår
(assuming that's what you mean) asparam=v%C3%A5r
.Oh dear! That's very much wrong.
%uXXXX
is a JavaScript-escape()
-style sequence only; it is wholly invalid to put in a URL. Safari is presumably trying to fix up the mistake by encoding the%
that isn't followed by a two-digit hex sequence with a%25
.Is ASP.NET generating this? If so, that's highly disappointing. How are you creating the
<form>
tag? If you're encoding the parameter manually, maybe you need to specify anEncoding
argument toHttpUtility.UrlEncode
? ie. anEncoding.UTF8
, or, if you really must havev%e5r
,new Encoding(1252)
(Windows code page 1252, Western European).