一、C#中的編碼
HttpUtility.HtmlDecode、HttpUtility.HtmlEncode與Server.HtmlDecode、Server.HtmlEncode與HttpServerUtility.HtmlDecode、HttpServerUtility.HtmlEncode的區別?
它們與下面一般手工寫的代碼有什么區別?
- public static string htmlencode(string str)
- {
- if (str == null || str == "")
- return "";
- str.Replace("<", "<");
- str.Replace(">", ">");
- str.Replace(" ", " ");
- str.Replace(" ", " ");
- str.Replace("/"", """);
- str.Replace("/'", "'");
- str.Replace("/n", "<br/>");
- return str;
- }
答案:
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;
它們只不過是為了調用方便,進行了封裝而已;
下面是一個非常簡單的替換測試代碼,測試結果看注釋:
- protected void Page_Load(object sender, EventArgs e)
- {
- TestChar("<"); //小於號 替換為 <
- TestChar(">"); //大於號 替換為 >
- TestChar(" "); //英文半角空格 替換為 不做替換;
- TestChar(" "); //中文全角空格 替換為 不做替換;
- TestChar("&"); //& 替換為 &
- TestChar("/'"); //單引號 替換為 ';
- TestChar("/""); //雙引號 替換為 "
- TestChar("/r"); //回車 替換為 不做替換;
- TestChar("/n"); //回車 替換為 不做替換;
- TestChar("/r/n"); //回車 替換為 不做替換;
- }
- protected void TestChar(String str)
- {
- Response.Write(Server.HtmlEncode(str));
- Response.Write("----------------------");
- Response.Write(HttpUility.HtmlEncode(str));
- Response.Write("<br/>");
- }
所以手工的替換方法還是很有必要的,處理一些HtmlEncode不支持的替換。
- public static string htmlencode(string str)
- {
- str.Replace("<", "<");
- str.Replace(">", ">");
- str.Replace(" ", " ");
- str.Replace(" ", " ");
- str.Replace("/'", "'");
- str.Replace("/"", """);
- str.Replace("/n", "<br/>");
- }
使用Reflector 查看 HttpUttility.HtmlEncode 的實現,我們就可以看到,它只考慮的五種情況,空格,回車是沒有處理的:
- public static unsafe void HtmlEncode(string value, TextWriter output)
- {
- if (value != null)
- {
- if (output == null)
- {
- throw new ArgumentNullException("output");
- }
- int num = IndexOfHtmlEncodingChars(value, 0);
- if (num == -1)
- {
- output.Write(value);
- }
- else
- {
- int num2 = value.Length - num;
- fixed (char* str = ((char*) value))
- {
- char* chPtr = str;
- char* chPtr2 = chPtr;
- while (num-- > 0)
- {
- chPtr2++;
- output.Write(chPtr2[0]);
- }
- while (num2-- > 0)
- {
- chPtr2++;
- char ch = chPtr2[0];
- if (ch <= '>')
- {
- switch (ch)
- {
- case '&':
- {
- output.Write("&");
- continue;
- }
- case '/'':
- {
- output.Write("'");
- continue;
- }
- case '"':
- {
- output.Write(""");
- continue;
- }
- case '<':
- {
- output.Write("<");
- continue;
- }
- case '>':
- {
- output.Write(">");
- continue;
- }
- }
- output.Write(ch);
- continue;
- }
- if ((ch >= '/x00a0') && (ch < 'ā'))
- {
- output.Write("&#");
- output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
- output.Write(';');
- }
- else
- {
- output.Write(ch);
- }
- }
- }
- }
- }
- }
二、JS中的編碼和解碼
- 一、escape/unescape
- escape:escape 方法返回一個包含 charstring 內容的字符串值(Unicode 格式)。所有空格、標點、 重音符號以及任何其他非 ASCII 字符都用 %xx 編碼替換,其中 xx 等於表示該字符的十六進制數
- unescape:從用 escape 方法編碼的 String 對象中返回已解碼的字符串
- 例外字符: @ * / +
- 二、encodeURI/decodeURI
- encodeURI:方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURI,則將返回初始的字符串。encodeURI 不對下列字符進行編碼:“:”、“/”、“;”和“?”。請使用 encodeURIComponent 對這些字符進行編碼
- decodeURI:從用encodeURI方法編碼的String對象中返回已解碼的字符串
- 例外字符:! @ # $ & * ( ) = : / ; ? + '
- 三、encodeURIComponent/decodeURIComponent
- encodeURIComponent:encodeURIComponent 方法返回一個已編碼的 URI。如果將編碼結果傳遞給decodeURIComponent,則將返回初始的字符串。因為 encodeURIComponent 方法將對所有字符編碼
- decodeURIComponent:從用encodeURIComponent方法編碼的String對象中返回已解碼的字符串
- 例外字符:! * ( ) '
本文摘自:http://blog.joycode.com/ghj/archives/2010/02/26/115894.joy