原文发布时间为:2010-07-10 —— 来源于本人的百度文章 [由搬家工具导入]

在Asp.net中可以使用Server.HTMLEncode和Server.URLEncode 将文本或URL的特殊字符编码,
但在控制台或Winform程序中没有办法使用到这些方法,

解决办法:

右击项目==》添加引用==》.NET==》System.Web==》确定

System.Web.HttpUtility.HtmlEncode(str);
System.Web.HttpUtility.HtmlDecode(str);

System.Web.HttpUtility.UrlEncode(str);
System.Web.HttpUtility.UrlDecode(str);



编码后得到的字串和用Server.URLEncode编码得到的不一样,那边接收到的也是乱码??

编码时可以指定编码的,如
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.Unicode);
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.UTF8);
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.GetEncoding( "GB2312 "));


解码也可以指定编码的
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.Unicode);
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.UTF8);
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.GetEncoding( "GB2312 "));

出现乱码是编码设置造成的。你可以试试不同的方法。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2022-01-16
  • 2022-01-17
  • 2022-12-23
猜你喜欢
  • 2022-02-01
  • 2021-06-14
  • 2021-07-18
  • 2021-12-22
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案