问题:

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(" <", "&lt;");
str = str.Replace(" ", "&nbsp;");
str = str.Replace(" ", " &nbsp;");
str = str.Replace("\"", "&quot;");
str = str.Replace("\'", "'");
str = str.Replace("\n", " <br/> ");
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 (<) &lt;
greater-than character (>) &gt;
ampersand character (&) &amp;
double-quote character (") &quot;
Any ASCII code character whose code is greater-than or equal to 0x80 &#<number>, where <number> is the ASCII character value.

如果是 DBCS 编码

  • All extended characters are converted.
  • Any ASCII code character whose code is greater-than or equal to 0x80 is converted to &#<number>, where <number> is the ASCII character value.
  • Half-width Katakana characters in the Japanese code page are not converted.

相关资料:

Server.HTMLEncode Method

http://msdn.microsoft.com/en-us/library/ms525347.aspx

在ASP.net 中情况也类似

下面是一个简单的替换测试代码,测试结果看之后的注释:

protected void Page_Load(object sender, EventArgs e)
{ TestChar("<"); // 小于号 替换 &lt;
TestChar(">"); // 大于号 替换 &gt;
TestChar("'"); // 单引号 替换 '
TestChar(" "); // 半角英文空格 不做替换
TestChar(" "); // 全角中文空格 不做替换
TestChar("&"); // & 替换 &amp;
TestChar("\""); // 英文双引号 替换 &quot;
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("<br />");
}

所以上面我们提到的常用替换方式还是非常有用的,他还处理了一些 HttpUtility.HtmlEncode 不支持的替换。

public static string htmlencode(string str)
{
if (str == null || str == "")
return "";
str = str.Replace(">", "&gt;");
str = str.Replace(" <", "&lt;");
str = str.Replace(" ", "&nbsp;"); // HttpUtility.HtmlEncode( 并不支持这个替换
str = str.Replace(" ", " &nbsp;"); // HttpUtility.HtmlEncode( 并不支持这个替换
str = str.Replace("\"", "&quot;");
str = str.Replace("\'", "'");
str = str.Replace("\n", " <br/> "); // 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, );
if (num == -)
{
output.Write(value);
}
else
{
int num2 = value.Length - num;
fixed (char* str = ((char*) value))
{
char* chPtr = str;
char* chPtr2 = chPtr;
while (num-- > )
{
chPtr2++;
output.Write(chPtr2[]);
}
while (num2-- > )
{
chPtr2++;
char ch = chPtr2[];
if (ch <= '>')
{
switch (ch)
{
case '&':
{
output.Write("&amp;");
continue;
}
case '\'':
{
output.Write("'");
continue;
}
case '"':
{
output.Write("&quot;");
continue;
}
case '<':
{
output.Write("&lt;");
continue;
}
case '>':
{
output.Write("&gt;");
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

转自:http://blog.joycode.com/ghj/archives/2010/02/26/115894.joy

几种 HtmlEncode 的区别(转发)的更多相关文章

  1. 几种HtmlEncode的区别(转)

    一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...

  2. (转)几种HtmlEncode的区别

    一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...

  3. Java中serialVersionUID的解释及两种生成方式的区别(转载)

    转载自:http://blog.csdn.net/xuanxiaochuan/article/details/25052057 serialVersionUID作用:        序列化时为了保持版 ...

  4. 链接属性rel=’external’、rel=’nofollow’、rel=’external nofollow’三种写法的区别

    链接属性rel='external'.rel='nofollow'.rel='external nofollow'三种写法的区别   大家应该都知道rel='nofllow'的作用,它是告诉搜索引擎, ...

  5. jsp中两种include的区别【转】

    引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...

  6. UIImage两种初始化的区别

    UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ...

  7. Linux 下Shell 脚本几种基本命令替换区别

    Shell 脚本几种基本命令替换区别 前言:因为工作需要,需要编写 shell script .编写大量 shell script 时,累计了大量经验,也让自己开始迷糊几种函数输出调用的区别.后面和 ...

  8. PHP中数组合并的两种方法及区别介绍

    PHP数组合并两种方法及区别 如果是关联数组,如下: 复制代码代码如下: $a = array( 'where' => 'uid=1', 'order' => 'uid', ); $b = ...

  9. 执行shell脚本的几种方法及区别

    执行shell脚本的几种方法及区别 http://blog.csdn.net/lanxinju/article/details/6032368 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...

随机推荐

  1. Weibo Crawler in Action

    1.要写一个微博爬虫,得分开几个模块来做: (1)模拟登录 (2)模拟浏览 (3)针对短时间内大量访问而引起怀疑的禁止登陆解决方案 (4)其他 (1)模拟登陆模块 前提:要模拟登录,得首先知道在登录微 ...

  2. leetcode oj s_06

    public class Solution { public static void main(String[] args) { String s = "PAYPALISHIRING&quo ...

  3. js 发红包

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. UTC+0800是什么意思

    <%@ language="javascript" %> <html> <body> <% var d=new Date() var h= ...

  5. fedora 16安装ByPass四网口网卡遇到的问题

    这个问题困扰了好几天,今天终于在大谷歌的帮助下,在这个网站http://blog.bwysystems.com/bwysystems/?p=16上找到了答案!还是国外的技术论坛强,在百度上搜遍了也没有 ...

  6. 异常处理与MiniDump详解(转)

    一.   综述 总算讲到MiniDump了. Dump有多有用我都无法尽数,基本上属于定位错误修复BUG的倚天剑.(日志可以算是屠龙刀)这些都是对于那些不是必出的BUG,放在外面运行的时候出现的BUG ...

  7. 存量数据处理结果查询.txt

    请求报文:<?xml version="1.0" encoding="UTF-8"?><PDL><PDL-Head>< ...

  8. C#读取文件为byte数组

    private byte[] FileContent(string fileName) { using (FileStream fs = new FileStream(fileName, FileMo ...

  9. stm32 usb数据接收与数据发送程序流程分析

    http://blog.csdn.net/u011318735/article/details/17424349 既然学习了USB,那就必须的搞懂USB设备与USB主机数据是怎么通讯的.这里主要讲设备 ...

  10. LeetCode 刷题记录(二)

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...