UrlEncode - Javascript 与 C#
我有一个需要一些参数的 URL。 这些参数的值可以是重音字符,因此我绝对需要对它们进行 UrlEncode。 奇怪的是,我发现 Javascript 和 .NET 的行为之间存在差异。
假设我尝试对单词“elephant”进行 UrlEncode。 在 JavaScript 中(根据此网站: http://www.albionresearch.com/misc/urlencode.php ),我得到以下信息:%E9l%E9phant。 这对我来说似乎是正确的。 但是,在 .NET 中,通过此调用 (System.Web.HttpUtility.UrlEncode("éléphant")),我得到“%c3%a9l%c3%a9phant”。 出了什么问题? 我缺少什么? 如果我想在.NET中获取%E9l%E9phant,我该怎么办?
谢谢!
I have a URL which requires some parameters. The values of those parameters can be accented characters, so I absolutely need to UrlEncode them. Strangely, I see a difference between the behavior or Javascript and .NET.
Let's pretend I try to UrlEncode the word "éléphant". In JavaScript (according to this WebSite: http://www.albionresearch.com/misc/urlencode.php), I get the following: %E9l%E9phant. This appears correct to me. However, in .NET with this call (System.Web.HttpUtility.UrlEncode("éléphant")) I get "%c3%a9l%c3%a9phant". What is wrong? What am I missing? What should I do if I want to get %E9l%E9phant in .NET?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
System.Web.HttpUtility.UrlEncode 将使用 UTF8 (我认为..)作为其默认编码器,您可以通过指定一个来更改它..
尽管最好指定实际的代码页或不指定什么,而不是依赖操作系统默认。
System.Web.HttpUtility.UrlEncode will use UTF8 (i think..) as its default encoder, you can change this by specifying one..
Though it may be preferable to specify an actual codepage or what not, instead of relying on the OS default.
那个页面是错误的。 JavaScript 也使用 UTF-8,就像 .NET 默认使用的那样。 亲自尝试一下:
现在的 URL 都是 UTF-8。 不要再尝试使用 cp1252。 UTF-8 是你的朋友。 相信 UTF-8!
That page is wrong. JavaScript also uses UTF-8, as .NET does by default. Try it yourself:
URLs today are UTF-8. Don't try to use cp1252 any more. UTF-8 is your friend. Trust UTF-8!
参考:
区别在于,一个是
UTF-8
URL 编码,而另一个是ANSI
URL 编码。在.NET中,尝试:
我得到
结果。 它与您为 JavaScript 提供的字符串匹配,但十六进制字符的情况除外。
Reference:
The difference is that one is the
UTF-8
URL Encoding whereas the other is giving you theANSI
URL Encoding.In .NET, try:
I get
as a result. That matches the string you provided for JavaScript, except for the Hex Character case.
仅当系统的当前代码页设置为西欧时,Encoding.Default 才适用。 现在,如果 Javascript 也与 .NET 编码器运行在同一台机器上,这一点可能很重要,但如果不是,那么您可以将代码页强制为西欧代码页:
Encoding.Default will only work for you if your system's current code page is set to Western European. Now this might be important if the Javascript is also running on the same machine as the .NET encoder, but if not, then you can force the code page to the Western European one:
您也可以尝试对它们进行 Base64 编码。
有一篇上一篇文章介绍了如何使用 JavaScript 进行此操作。
至于.NET,有很多例子展示了如何做到这一点。 这是我在 Google 上发现的一个。
当然,字符串可能比 urlencoding 稍大一些。 然而,它们会被混淆,这可能是一个优势,具体取决于应用程序。
You could also try base64 encoding them.
There's a previous post that deals with doing it in JavaScript.
As for in .NET, there are many examples showing how to do it. Here's one I found off Google.
Of course, the strings might be a bit larger than urlencoding. However, they'll be obfuscated which can be an advantage depending on the application.