几种 HtmlEncode 的区别(转发)
问题:
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", " <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 (<) | < |
| greater-than character (>) | > |
| ampersand character (&) | & |
| double-quote character (") | " |
| 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("<"); // 小于号 替换 <
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("<br />");
}
所以上面我们提到的常用替换方式还是非常有用的,他还处理了一些 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", " <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("&");
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
转自:http://blog.joycode.com/ghj/archives/2010/02/26/115894.joy
几种 HtmlEncode 的区别(转发)的更多相关文章
- 几种HtmlEncode的区别(转)
一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...
- (转)几种HtmlEncode的区别
一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...
- Java中serialVersionUID的解释及两种生成方式的区别(转载)
转载自:http://blog.csdn.net/xuanxiaochuan/article/details/25052057 serialVersionUID作用: 序列化时为了保持版 ...
- 链接属性rel=’external’、rel=’nofollow’、rel=’external nofollow’三种写法的区别
链接属性rel='external'.rel='nofollow'.rel='external nofollow'三种写法的区别 大家应该都知道rel='nofllow'的作用,它是告诉搜索引擎, ...
- jsp中两种include的区别【转】
引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...
- UIImage两种初始化的区别
UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ...
- Linux 下Shell 脚本几种基本命令替换区别
Shell 脚本几种基本命令替换区别 前言:因为工作需要,需要编写 shell script .编写大量 shell script 时,累计了大量经验,也让自己开始迷糊几种函数输出调用的区别.后面和 ...
- PHP中数组合并的两种方法及区别介绍
PHP数组合并两种方法及区别 如果是关联数组,如下: 复制代码代码如下: $a = array( 'where' => 'uid=1', 'order' => 'uid', ); $b = ...
- 执行shell脚本的几种方法及区别
执行shell脚本的几种方法及区别 http://blog.csdn.net/lanxinju/article/details/6032368 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...
随机推荐
- Mac下用命令行直接批量转换文本编码到UTF8
由于近期在Mac下写Android程序,下载的一些Demo由于编码问题源码里的汉字出现乱码,文件比较多,所以想批量解决下文件的编码问题. Mac下有以下两种方式可以解决: A. 文件名的编码:Mac的 ...
- iOS tableview 优化总结
根据网络上的优化方法进行了总括.并未仔细进行语言组织.正在这些优化方法进行学习,见另一篇文章 提高app流畅度 1.cell子控件创建写在 initWithStyle:reuseIdentifier ...
- T-SQL游标
游标是面向行的,它会使开发人员变懒,懒得去想用面向集合的查询方式实现某些功能. 在性能上,游标会迟更多的内存,减少可用的并发,占用带宽,锁定资源,当然还有更多的代码量. 用一个比喻来说明为什么游标会占 ...
- Spark的优点
Spark的一站式解决方案,非常之具有吸引力,毕竟啊,任何公司都想用统一的平台去处理遇到的问题,减少开发和维护的人力成本和部署平台的物力成本. 当然,Spark并没有以牺牲性能为代价.相反,在性能方面 ...
- 将服务器返回的URL或者网址截取出来特定的字符,然后将字符返回,一般根据返回的字符判断用户是否登录等即时状态
1.用NSScanner过滤掉url中开头或者尾部存在的空格 2.用NSScanner的 setCharactersToBeSkipped方法忽略指定的字符集 3.用scanUpToString扫描去 ...
- JavaIO(06)文件复制
文件复制一般是采用两种方式进行操作: 1:将源文件中的内容全部读取到内存中,并一次性的写入到目标文件中:(不常用这种方式) 2:不将源文件中的内容全部读取进来,而是采用边读边写的方式: 实例01: ...
- MAT(3)获取dump文件
方式一:添加启动参数 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\Java\dump 生成的文件例如:java_pid2080.hprof ...
- eclipse查看 rt.jar 源码
源码位置: %JAVA_HOME%/src.zip step1: step2: step3:
- 关于TabControl的Trigger【项目】
我有一个TabControl <TabControl x:Name="ToolSystemSection" Grid.Row="4" ContentTem ...
- JedisPoolConfig配置
JedisPoolConfig config = new JedisPoolConfig(); //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true config. ...