C#中實現UrlEncode和UrlDecode


  • 有時需要進行url編碼、解碼,比如從html中撈數據,有可能>、&等字符會被編碼成>等。
  • WinForm中默認沒有引入System.Web,因此要現在項目中引入依賴
    • System.Web.HttpUtility.HtmlEncode(str);
    • System.Web.HttpUtility.HtmlDecode(str);
    • System.Web.HttpUtility.UrlEncode(str); 
    • System.Web.HttpUtility.UrlDecode(str);
  • 編碼、解碼時可以指定編碼,否則會看到亂碼,如:
    • 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 "));
    • 等 


免責聲明!

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



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