URL encode ASCII/UTF16 characters

假装没事ソ 提交于 2019-12-06 12:03:30

The reason is not that .NET uses UTF-16 encoded strings. The reason is that the UrlEncode(string) overload uses UTF-8 by default, and %C3%A2 is the correct UTF-8 encoding of â:

The HttpUtility.UrlEncode method uses UTF-8 encoding by default. Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter.

If you prefer a different encoding (for example Latin-1 or Codepage 1252, where â corresponds to %E2), you can use another overload that allows you to specify an encoding:

var x = HttpUtility.UrlEncode("â", Encoding.GetEncoding(1252));

Just pass the correct encoding to System.Web.HttpUtility.UrlEncode (Windows-1252, Windows-1254, ISO-8859-1, ISO-8859-9 etc. whichever appropriate)

 var s = HttpUtility.UrlEncode("â", Encoding.GetEncoding("Windows-1252"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!