问题:
httpUtility.HTMLDecode ,httpUtility.HTMLEncode 与 Server.HTMLDecode ,Server.HTMLEncode 与 httpServerUtility.HTMLDecode , httpServerUtility.HTMLEncode 有什么区别?
他们与下面一般手工写的代码有什么不一样的?
public static string HTMLencode(string str) { if (str == null || str == "") return ""; str = str.Replace(">",">"); str = str.Replace(" <","<"); str = str.Replace(" "," "); str = str.Replace(" "," "); str = str.Replace("/"","""); str = str.Replace("/'","'"); str = str.Replace("/n","
"); return str; }
答案:
HTMLEncode: 将 HTML 源文件中不允许出现的字符进行编码,通常是编码以下字符"<"、">"、"&" 等。
HTMLDecode: 刚好跟 HTMLEncode 相关,解码出来原本的字符。
httpServerUtility 实体类的 HTMLEncode 方法 是一种简便方式,用于在运行时从 ASP.NET Web 应用程序访问 System.Web.httpUtility.HTMLEncode 方法。httpServerUtility 实体类的 HTMLEncode 方法 在内部使用 System.Web.httpUtility.HTMLEncode 对字符串进行编码。
Server.HTMLEncode 其实就是 System.Web.UI.Page 类封装的 httpServerUtility 实体类的 HTMLEncode 方法; System.Web.UI.Page 类有这样的一个属性: public httpServerUtility Server { get; }
所以我们可以认为:
Server.HTMLDecode = httpServerUtility 实体类的 HTMLDecode 方法 = httpUtility.HTMLDecode ;
Server.HTMLEncode = httpServerUtility 实体类的 HTMLEncode 方法 = httpUtility.HTMLEncode ;
他们只不过是为了调用方便,做了封装而已。
在 ASP 中, Server.HTMLEncode Method 过滤的字符描述如下:
如果字符串不是 DBCS 编码。这个方法将转换下面字符:
less-than character (<) | < |
greater-than character (>) | > |
ampersand character (&) | & |
double-quote character (") | " |
Any ASCII code character whose code is greater-than or equal to 0x80 | &# |
如果是 DBCS 编码
All extended characters are converted. Any ASCII code character whose code is greater-than or equal to 0x80 is converted to &#相关资料:
Server.HTMLEncode Method
http://msdn.microsoft.com/en-us/library/ms525347.aspx
在ASP.net 中情况也类似
下面是一个简单的替换测试代码,测试结果看之后的注释:
protected voID Page_Load(object sender,EventArgs e){ TestChar("<"); // 小于号 替换 < TestChar(">"); // 大于号 替换 > TestChar("'"); // 单引号 替换 ' TestChar(" "); // 半角英文空格 不做替换 TestChar(" "); // 全角中文空格 不做替换 TestChar("&"); // & 替换 & TestChar("/""); // 英文双引号 替换 " TestChar("/n"); // 回车 不做替换 TestChar("/r"); // 回车 不做替换 TestChar("/r/n"); // 回车 不做替换}public voID TestChar(string t){ Response.Write(Server.HTMLEncode(t)); Response.Write("__"); Response.Write(httpUtility.HTMLEncode(t)); Response.Write("
");}
所以上面我们提到的常用替换方式还是非常有用的,他还处理了一些 httpUtility.HTMLEncode 不支持的替换。
public static string HTMLencode(string str){ if (str == null || str == "") return ""; str = str.Replace(">",">"); str = str.Replace(" <","<"); str = str.Replace(" "," "); // httpUtility.HTMLEncode( 并不支持这个替换 str = str.Replace(" "," "); // httpUtility.HTMLEncode( 并不支持这个替换 str = str.Replace("/"","""); str = str.Replace("/'","'"); str = str.Replace("/n","
"); // httpUtility.HTMLEncode( 并不支持这个替换 return str;}
我们使用 Reflector 查看 httpUtility.HTMLEncode 的实现,我们就可以看到,它只考虑的五种情况,空格,回车是没有处理的:
使用 Reflector 查看 httpUtility.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); } } } } }}
参考资料:
httpUtility.HTMLDecode与Server.HTMLDecode区别
http://topic.csdn.net/u/20090220/11/110c8079-1632-418a-b43b-3ddb2f0a06e2.html
詳細解說幾個建置網站時常用的編碼方法
http://blog.miniasp.com/?tag=/htmlencode
用于 Silverlight 的 .NET Framework 类库httpUtility.HTMLEncode 方法
http://msdn.microsoft.com/zh-cn/library/system.windows.browser.httputility.htmlencode(VS.95).aspx
httpUtility.HTMLEncode() and httpServerUtility.HTMLEncode() do not encode all non-ASCII characters
https://connect.microsoft.com/VisualStudio/feedback/details/102251/httputility-htmlencode-and-httpserverutility-htmlencode-do-not-encode-all-non-ascii-characters?wa=wsignin1.0
以上是内存溢出为你收集整理的几种 HtmlEncode 的区别全部内容,希望文章能够帮你解决几种 HtmlEncode 的区别所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)