几种HtmlEncode的区别(转)
一、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; } [c-sharp]答案:
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/>"); } [c-sharp]所以手工的替換方法還是很有必要的,處理一些HtmlEncode不支持的替換。
public static string htmlencode(string str) { str.Replace("<", "<"); str.Replace(">", ">"); str.Replace(" ", " "); str.Replace(" ", " "); str.Replace("/'", "'"); str.Replace("/"", """); str.Replace("/n", "<br/>"); } [c-sharp]使用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); } } } } } } [c-sharp]二、JS中的編碼和解碼
原文:http://blog.csdn.net/wd330260402/article/details/5977989
轉載于:https://www.cnblogs.com/MirageFox/p/4814164.html
總結
以上是生活随笔為你收集整理的几种HtmlEncode的区别(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Xcode的SVN提示The reque
- 下一篇: Linux 文件权限详解 含义和修改和安