一、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中的编码和解码

  1. 一、escape/unescape
  2. escape:escape 方法返回一个包含 charstring 内容的字符串值(Unicode 格式)。所有空格、标点、 重音符号以及任何其他非 ASCII 字符都用 %xx 编码替换,其中 xx 等于表示该字符的十六进制数
  3. unescape:从用 escape 方法编码的 String 对象中返回已解码的字符串
  4. 例外字符: @ * / +
  5. 二、encodeURI/decodeURI
  6. encodeURI:方法返回一个已编码的 URI。如果将编码结果传递给 decodeURI,则将返回初始的字符串。encodeURI 不对下列字符进行编码:“:”、“/”、“;”和“?”。请使用 encodeURIComponent 对这些字符进行编码
  7. decodeURI:从用encodeURI方法编码的String对象中返回已解码的字符串
  8. 例外字符:! @ # $ & * ( ) = : / ; ? + '
  9. 三、encodeURIComponent/decodeURIComponent
  10. encodeURIComponent:encodeURIComponent 方法返回一个已编码的 URI。如果将编码结果传递给decodeURIComponent,则将返回初始的字符串。因为 encodeURIComponent 方法将对所有字符编码
  11. decodeURIComponent:从用encodeURIComponent方法编码的String对象中返回已解码的字符串
  12. 例外字符:! * ( ) '

原文:http://blog.csdn.net/wd330260402/article/details/5977989

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

  1. (转)几种HtmlEncode的区别

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

  2. 几种 HtmlEncode 的区别(转发)

    问题: HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode  与  Server.HtmlDecode ,Server.HtmlEncode  与 HttpS ...

  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. 删除ecshop云服务及授权关于官方等信息

    一.删除[云服务中心] 删除/admin/cloud.php 删除/admin/templates/menu.htm中以下代码 Ajax.call('cloud.php?is_ajax=1>ac ...

  2. pipe row的用法, Oracle split 函数写法.

    为了让 PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合在可以返回前,必须进行 ...

  3. C++Lua配置

    1.先从lua官网下载lua新版本http://www.lua.org/,我这里以lua-5.3.0.tar.gz为例,大小不到300kb 2.解压后出现如下图 3.在vs2013新建工程静态库类型( ...

  4. 20145227 《Java程序设计》第8周学习总结

    20145227 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO即New IO.java从JDK1.4开始提供了NIO,在JAVA SE 7 中又 ...

  5. cookie学习

    cookie是储存于访问者的计算机中的变量,每当同一台计算机通过浏览器请求某个页面时,就会发送这个cookie,可以使用javascript来创建和取回cookie的值. 创建和存储cookie 首先 ...

  6. 【转】cvs2svn 把CVS档案库转换为SVN档案库

    转载地址:http://jackdown.blog.sohu.com/66646130.html 在linux下的操作 1).安装 下载:Python 2.0   地址:http://www.pyth ...

  7. LA 3907 Puzzle

    问题描述:先给你s个禁止串,求不包含禁止串的最长串,如果存在,打印字典序最大. 数据范围:s <= 1000, 禁止串长度不超过50. 分析:不匹配问题实际上等同于匹配问题.假设我们已经有满足条 ...

  8. ExecutorService - 10个技巧和窍门

    ExecutorService已经成为Java并发编程中常用的基础库,几乎所有到线程 任务等执行都要委托ExecutorService.下面是使用过程中10个技巧和窍门. 1.为线程池和线程取名 当我 ...

  9. springmvc配置文件-1

    项目1: web.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?& ...

  10. IOS文字属性备注

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...