C# 幾種HtmlEncode的區別


一、C#中的編碼

HttpUtility.HtmlDecodeHttpUtility.HtmlEncodeServer.HtmlDecode、Server.HtmlEncodeHttpServerUtility.HtmlDecode、HttpServerUtility.HtmlEncode的區別?

它們與下面一般手工寫的代碼有什么區別?

 

[c-sharp]  view plain copy
 
  1. public static string htmlencode(string str)  
  2. {  
  3.     if (str == null || str == "")  
  4.         return "";  
  5.   
  6.     str.Replace("<""<");  
  7.     str.Replace(">"">");  
  8.     str.Replace(" "" ");  
  9.     str.Replace(" ""  ");  
  10.     str.Replace("/"", """);  
  11.     str.Replace("/'""'");  
  12.     str.Replace("/n""<br/>");  
  13.   
  14.     return str;  
  15. }  

 

答案:

HtmlEncode:是將html源文件中不容許出現的字符進行編碼,通常是編碼以下字符:"<"、">"、"&"、"""、"'"等;

HtmlDecode:跟HtmlEncode恰好相反,是解碼出原來的字符;

 

HttpServerUtility實體類的HtmlEncode(HtmlDecode)的簡便方式,用於在運行時從ASP.NET Web應用程序訪問System.Web.HttpUtility.HtmlEncode(HtmlDecode)方法,HttpServerUtility實體類的HtmlEncode(HtmlDecode)方法在內部是使用System.Web.HttpUtility.HtmlEncode(HtmlDecode)方法對字符進行編碼(解碼)的;

 

 

Server.HtmlEncode(Server.HtmlDecode)其實是System.Web.UI.Page類封裝了HttpServerUtility實體類的HtmlEncode(HtmlDecode)的方法;

System.Web.UI.Page類有這樣一個屬性:public HttpServerUtility Server{get;}

 

 

所以可以認為:

Server.HtmlEncode=HttpServerUtility實體類的HtmlEncode方法=HttpUtility.HtmlEncode;

Server.HtmlDecode=HttpServerUtility實體類的HtmlDecode方法=HttpUtility.HtmlDecode;

它們只不過是為了調用方便,進行了封裝而已;

 

下面是一個非常簡單的替換測試代碼,測試結果看注釋: 

 

[c-sharp]  view plain copy
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     TestChar("<");   //小於號        替換為      <         
  4.     TestChar(">");   //大於號        替換為      >  
  5.     TestChar(" ");    //英文半角空格        替換為      不做替換;  
  6.     TestChar(" ");  //中文全角空格        替換為      不做替換;  
  7.     TestChar("&");   //&        替換為      &  
  8.     TestChar("/'");   //單引號        替換為      ';  
  9.     TestChar("/"");   //雙引號        替換為      "  
  10.     TestChar("/r");   //回車        替換為      不做替換;  
  11.     TestChar("/n");   //回車        替換為      不做替換;  
  12.     TestChar("/r/n");   //回車        替換為      不做替換;  
  13. }  
  14. protected void TestChar(String str)  
  15. {  
  16.     Response.Write(Server.HtmlEncode(str));  
  17.     Response.Write("----------------------");  
  18.     Response.Write(HttpUility.HtmlEncode(str));  
  19.     Response.Write("<br/>");  
  20. }  

 

 

 

所以手工的替換方法還是很有必要的,處理一些HtmlEncode不支持的替換。

 

[c-sharp]  view plain copy
 
  1. public static string htmlencode(string str)  
  2. {  
  3.     str.Replace("<""<");  
  4.     str.Replace(">"">");  
  5.     str.Replace(" "" ");  
  6.     str.Replace(" "" ");  
  7.     str.Replace("/'""'");  
  8.     str.Replace("/"", """);  
  9.     str.Replace("/n""<br/>");  
  10. }  

 

 

使用Reflector 查看 HttpUttility.HtmlEncode 的實現,我們就可以看到,它只考慮的五種情況,空格,回車是沒有處理的:

 

[c-sharp]  view plain copy
 
  1. public static unsafe void HtmlEncode(string value, TextWriter output)  
  2. {  
  3.     if (value != null)  
  4.     {  
  5.         if (output == null)  
  6.         {  
  7.             throw new ArgumentNullException("output");  
  8.         }  
  9.         int num = IndexOfHtmlEncodingChars(value, 0);  
  10.         if (num == -1)  
  11.         {  
  12.             output.Write(value);  
  13.         }  
  14.         else  
  15.         {  
  16.             int num2 = value.Length - num;  
  17.             fixed (char* str = ((char*) value))  
  18.             {  
  19.                 char* chPtr = str;  
  20.                 char* chPtr2 = chPtr;  
  21.                 while (num-- > 0)  
  22.                 {  
  23.                     chPtr2++;  
  24.                     output.Write(chPtr2[0]);  
  25.                 }  
  26.                 while (num2-- > 0)  
  27.                 {  
  28.                     chPtr2++;  
  29.                     char ch = chPtr2[0];  
  30.                     if (ch <= '>')  
  31.                     {  
  32.                         switch (ch)  
  33.                         {  
  34.                             case '&':  
  35.                             {  
  36.                                 output.Write("&");  
  37.                                 continue;  
  38.                             }  
  39.                             case '/'':  
  40.                             {  
  41.                                 output.Write("'");  
  42.                                 continue;  
  43.                             }  
  44.                             case '"':  
  45.                             {  
  46.                                 output.Write(""");  
  47.                                 continue;  
  48.                             }  
  49.                             case '<':  
  50.                             {  
  51.                                 output.Write("<");  
  52.                                 continue;  
  53.                             }  
  54.                             case '>':  
  55.                             {  
  56.                                 output.Write(">");  
  57.                                 continue;  
  58.                             }  
  59.                         }  
  60.                         output.Write(ch);  
  61.                         continue;  
  62.                     }  
  63.                     if ((ch >= '/x00a0') && (ch < 'ā'))  
  64.                     {  
  65.                         output.Write("&#");  
  66.                         output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));  
  67.                         output.Write(';');  
  68.                     }  
  69.                     else  
  70.                     {  
  71.                         output.Write(ch);  
  72.                     }  
  73.                 }  
  74.             }  
  75.         }  
  76.     }  
  77. }   

 

 

二、JS中的編碼和解碼

 

[c-sharp] view plaincopy
 
  1. 一、escape/unescape  
  2.     escape:escape 方法返回一個包含 charstring 內容的字符串值(Unicode 格式)。所有空格、標點、 重音符號以及任何其他非 ASCII 字符都用 %xx 編碼替換,其中 xx 等於表示該字符的十六進制數  
  3.     unescape:從用 escape 方法編碼的 String 對象中返回已解碼的字符串  
  4.     例外字符: @ * / +  
  5.   
  6. 二、encodeURI/decodeURI  
  7.     encodeURI:方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURI,則將返回初始的字符串。encodeURI 不對下列字符進行編碼:“:”、“/”、“;”和“?”。請使用 encodeURIComponent 對這些字符進行編碼  
  8.     decodeURI:從用encodeURI方法編碼的String對象中返回已解碼的字符串  
  9.     例外字符:! @ # $ & * ( ) = : / ; ? + '  
  10.   
  11. 三、encodeURIComponent/decodeURIComponent  
  12.     encodeURIComponent:encodeURIComponent 方法返回一個已編碼的 URI。如果將編碼結果傳遞給decodeURIComponent,則將返回初始的字符串。因為 encodeURIComponent 方法將對所有字符編碼  
  13.     decodeURIComponent:從用encodeURIComponent方法編碼的String對象中返回已解碼的字符串  
  14.     例外字符:! * ( ) '  

 

 

本文摘自:http://blog.joycode.com/ghj/archives/2010/02/26/115894.joy


免責聲明!

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



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