UrlEncode - Javascript vs. C# [duplicate]

2019-02-22 04:32发布

问题:

This question already has an answer here:

  • Does C# have an equivalent to JavaScript's encodeURIComponent()? 7 answers

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!

回答1:

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

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.



回答2:

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

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

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!



回答3:

Reference:

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

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

In .NET, try:

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.



回答4:

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:

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


回答5:

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.