UrlEncode - Javascript 与 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/514688/
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-04 06:22:29  来源:igfitidea点击:

UrlEncode - Javascript vs. C#

c#javascriptencodingurlencode

提问by Martin

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.

我有一个需要一些参数的 URL。这些参数的值可以是重音字符,所以我绝对需要对它们进行 UrlEncode。奇怪的是,我发现行为或 Javascript 和 .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?

让我们假设我尝试对单词“éléphant”进行 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 应该怎么做?

Thanks!

谢谢!

采纳答案by Sciolist

System.Web.HttpUtility.UrlEncode will use UTF8 (i think..) as its default encoder, you can change this by specifying one..

System.Web.HttpUtility.UrlEncode 将使用 UTF8(我认为..)作为其默认编码器,您可以通过指定一个..

System.Web.HttpUtility.UrlEncode("éléphant", Encoding.Default); // %e9l%e9phant

Though it may be preferable to specify an actual codepage or what not, instead of relying on the OS default.

虽然最好指定一个实际的代码页或什么不是,而不是依赖于操作系统的默认值。

回答by EndangeredMassa

Reference:

参考:

js:       %E9 l     %E9 phant
.net: %c3 %a9 l %c3 %a9 phant

The difference is that one is the UTF-8URL Encoding whereas the other is giving you the ANSIURL Encoding.

不同之处在于,一个是UTF-8URL 编码,而另一个是为您提供ANSIURL 编码。

In .NET, try:

在 .NET 中,尝试:

Dim a As String = System.Web.HttpUtility.UrlEncode( _
    "éléphant", _
    Text.Encoding.Default)

I get

我得到

    %e9 l     %e9 phant

as a result. That matches the string you provided for JavaScript, except for the Hex Character case.

其结果。这与您为 JavaScript 提供的字符串匹配,十六进制字符大小写除外。

回答by scwagner

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:

Encoding.Default 仅适用于您系统的当前代码页设置为西欧的情况。现在,如果 Javascript 也与 .NET 编码器在同一台机器上运行,这可能很重要,但如果不是,那么您可以将代码页强制为西欧:

System.Web.HttpUtility.UrlEncode("éléphant", Encoding.GetEncoding(1252));

回答by cdmckay

You could also try base64 encoding them.

您也可以尝试对它们进行 base64 编码。

There's a previous postthat deals with doing it in JavaScript.

之前有一篇文章介绍了如何在 JavaScript 中执行此操作。

As for in .NET, there are many examples showing how to do it. Here's one I foundoff Google.

至于在 .NET 中,有很多示例展示了如何做到这一点。这是我从 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.

当然,字符串可能比 urlencoding 大一点。但是,它们会被混淆,这可能是一个优势,具体取决于应用程序。

回答by bobince

In JavaScript (according to this WebSite: http://www.albionresearch.com/misc/urlencode.php), I get the following: %E9l%E9phant.

在 JavaScript 中(根据本网站:http: //www.albionresearch.com/misc/urlencode.php),我得到以下信息:%E9l%E9phant。

That page is wrong. JavaScript also uses UTF-8, as .NET does by default. Try it yourself:

那个页面错了。JavaScript 也使用 UTF-8,就像 .NET 默认使用的那样。自己试试:

javascript:alert(encodeURIComponent('éléphant'))
%C3%A9l%C3%A9phant

URLs today are UTF-8. Don't try to use cp1252 any more. UTF-8 is your friend. Trust UTF-8!

今天的 URL 是 UTF-8。不要再尝试使用 cp1252。UTF-8 是你的朋友。相信 UTF-8!