问题:

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. 第二百三十天 how can I 坚持

    上周日去蟒山摘的松子吗?应该是松子吧,裂开了呢.为啥呢.原来博客园可以上传图片,只是上传起来好费劲啊. 今天程哥问给我分的活多不多,我竟然说了句好多,哎.其实很多问题可以用还好来回答,还好,还行,哈哈 ...

  2. Chef

    Chef是一个渐渐流行的部署大.小集群的自动化管理平台.Chef可以用来管理一个传统的静态集群,也可以和EC2或者其他的云计算提供商一起使用.Chef用cookbook作为最基本的配置单元,可以被泛化 ...

  3. cocos2d-x使用python创建vs模板

    cocos2d-x 2.2推荐使用create_project.py创建工程,所有的平台都可以通过这个python文件创建工程.这个文件位置在源码cocos2d-x-2.2.2\tools\proje ...

  4. [原创]Devexpress XtraReports 系列 9 创建邮件合并报表

    昨天发表了Devexpress XtraReports系列第八篇[原创]Devexpress XtraReports 系列 8 创建Drill-Through报表,今天我们继续. 今天的主题是创建邮件 ...

  5. git会议分享

    git add . git add -A git add common.scss   只迁入某个文件 git pull h5 远程的:分支    这样就成功拉取一个新分支了 git push h5(远 ...

  6. 红米手机拍照效果测评(对比小米2A)

    小米相关的产品一向都很很受用户的欢迎,一个就是实惠,另一个就是配置还不错.近期小米推出的红米手机可谓是先声夺人,关注度异常火爆.今天刚抢的红米快递寄到了,来测试下红米手机的拍照表现,800万像素怎么样 ...

  7. IIS6.0禁止用户下载txt文件

    服务器win2003+IIS6.0,在IIS上限制下载.txt文件,方法比较简单,在IIS的应用程序扩展名映射中为资源扩展名指定一个错误的可执行文件即可. 打开记事本,直接将空内容保存为C:\WIND ...

  8. CCPC总结

    [印象·南阳] 10月15日出发,威海—烟台—郑州—南阳,一路上欢声笑语,从谁是卧底到各类纸牌游戏,也是欢乐.在从郑州到南阳的车上,对面的好像是河南当地的学长,感叹道工作不易的样子,说还是学生时代最为 ...

  9. linux中less命令使用

    less与cat和more的区别: cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面 ...

  10. Java 线程池学习

    Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...