几种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, );
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);
}
}
}
}
}
}
[c-sharp]
二、JS中的编码和解码
- 一、escape/unescape
- escape:escape 方法返回一个包含 charstring 内容的字符串值(Unicode 格式)。所有空格、标点、 重音符号以及任何其他非 ASCII 字符都用 %xx 编码替换,其中 xx 等于表示该字符的十六进制数
- unescape:从用 escape 方法编码的 String 对象中返回已解码的字符串
- 例外字符: @ * / +
- 二、encodeURI/decodeURI
- encodeURI:方法返回一个已编码的 URI。如果将编码结果传递给 decodeURI,则将返回初始的字符串。encodeURI 不对下列字符进行编码:“:”、“/”、“;”和“?”。请使用 encodeURIComponent 对这些字符进行编码
- decodeURI:从用encodeURI方法编码的String对象中返回已解码的字符串
- 例外字符:! @ # $ & * ( ) = : / ; ? + '
- 三、encodeURIComponent/decodeURIComponent
- encodeURIComponent:encodeURIComponent 方法返回一个已编码的 URI。如果将编码结果传递给decodeURIComponent,则将返回初始的字符串。因为 encodeURIComponent 方法将对所有字符编码
- decodeURIComponent:从用encodeURIComponent方法编码的String对象中返回已解码的字符串
- 例外字符:! * ( ) '
几种HtmlEncode的区别(转)的更多相关文章
- (转)几种HtmlEncode的区别
一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...
- 几种 HtmlEncode 的区别(转发)
问题: HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode 与 Server.HtmlDecode ,Server.HtmlEncode 与 HttpS ...
- 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 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...
随机推荐
- 原来样式改变不了(input type="file")
label { background-color: #979fa8; color: #fff; display: inline-block; padding: .8rem 4rem; cursor: ...
- ACM第四站————最小生成树(克鲁斯卡尔算法)
都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...
- Adam 演示demo内容整理
在这个6个多G的演示demo中,还是发现了不少东西. 这篇文章八卦向的东西比较多,不过支持abc格式的话,做Cutscene一下子多了很多可以用的东西. 1.在插件目录下发现了ABC格式的导入dll. ...
- linux socket高性能服务器处理框架
这个博客很多东西 http://blog.csdn.net/luozhonghua2014/article/details/37041765 思考一种高性能的服务器处理框架 1.首先需要一个内存池 ...
- php基础复习(一)smarty模板
一.基本配置第一步:下载smarty:官网www.smarty.net第二步:加载类文件和更改配置 1. //加载类文件 require_once '../libs/Smarty.class.php' ...
- tee 命令基本使用方法、输出到多个文件
功能说明:读取标准输入的数据,并将其内容输出成文件.语 法:tee [-ai][--help][--version][文件...]补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出 ...
- Bitmap的分析与使用.md
https://github.com/GeniusVJR/LearningNotes/blob/master/Part1/Android/Bitmap的分析与使用.md
- 【Linux】用grep在文档中查找内容
有时候,我们需要在文档中查找一些内容,常用grep.它在文档查找相关内容并输出匹配行. > 查找某关键字 在system.log中,查找包含keyword的行 grep 'keyword' sy ...
- EXCEL中讲 10分10秒转换成610秒
前几天宝贝跟我打赌100W说我20天给她打电话不到10小时,我说绝对超过10小时了,但是由于宝贝的赖皮死活不承认,所以我被迫掉出通话记录,拿到通话记录我有点小郁闷,因为通话记录里的时间格式00分00秒 ...
- Linux有问必答:如何在Linux中修改环境变量PATH
提问: 当我试着运行一个程序时,它提示“command not found”. 但这个程序就在/usr/local/bin下.我该如何添加/usr/local/bin到我的PATH变量下,这样我就可以 ...