C#版winform實現UrlEncode


在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 "));


MessageBox.Show(System.Web.HttpUtility.UrlEncode(s,System.Text.Encoding.GetEncoding("GB2312")).ToUpper());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM