使用 unicode 字符进行 301 重定向 - C#
我需要对可能包含 Unicode 字符的 URL 进行 301 重定向。
HttpUtility.UrlEncode 没有执行我需要的操作,因为如果我对整个 URL 进行编码,它就会对任何“:”或“/”进行编码
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(另外:http://www.%e0%a4%b9%e0%a4%bf% e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com 似乎在 Firefox 或 IE 中不起作用,但在 Chrome 中却可以)
我唯一的其他事情可以想到的是对 URL 的不同部分进行编码,这样协议就不会被编码。
I need to do a 301 redirect on a URL that may have Unicode characters in it.
HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)
Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查看 RFC 3490,其中详细介绍了如何正确编码国际域名-- 这也是为什么当你只编码域部分时它只能在 Chrome 中工作)
You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)
所以我想出了一个几乎100%解决这个问题的方法。感谢 Rowland Shaw 和 Rup 为我指明了 IDN 的方向。
我尝试使用 IdnMapper,其函数 GetAscii 会将 unicode 域名转换为 punycode,但我没有将域名与 URL 的其余部分分开。我尝试将 url 放入 Uri 对象中,但如果 url 包含 unicode 字符,我会收到 UriFormatException。
这导致我: http://msdn .microsoft.com/en-us/library/system.uri(v=VS.90).aspx
它告诉我们如何使 Uri 类接受 unicode 并进行 IDN 和 IRI 转换。它说您必须向 .NET 2.0 machine.config 文件添加一些内容,但您可以将该行放入 web.config 中,它就会起作用。
在我让 Uri 使用 unicode 后,我拼凑了 url 并进行了重定向:
这适用于 Chrome 和 Firefox 3.6,但在 IE8 中失败。我仍在尝试解决该问题,如果找到解决方案,我会在这里更新。
So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.
I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.
That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx
which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.
After I got the Uri working with unicode, I pieced together the url and did a redirect:
This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.