UrlEncode - JavaScript与C# [英] UrlEncode - Javascript vs. C#

查看:159
本文介绍了UrlEncode - JavaScript与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要一些参数的URL。这些参数的值可以是重音字符,所以我绝对需要UrlEncode它们。奇怪的是,我看到行为或JavaScript和.NET之间有区别。



让我们假装我尝试UrlEncodeéléphant。在JavaScript中(根据本网站: http://www.albionresearch.com/misc/urlencode.php ),我得到以下内容:%E9l%E9phant。这对我来说是正确的。然而,在.NET中使用这个调用(System.Web.HttpUtility.UrlEncode(éléphant))我得到%c3%a9l%c3%a9phant。有什么问题?我失踪了什么如果我想在.NET中获得%E9l%E9phant,该怎么办?



谢谢!

解决方案

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

  System.Web.HttpUtility.UrlEncode(éléphant,Encoding.Default); //%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!

解决方案

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.

这篇关于UrlEncode - JavaScript与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆