一、C#中的编码

HttpUtility.HtmlDecode、HttpUtility.HtmlEncode与Server.HtmlDecode、Server.HtmlEncode与HttpServerUtility.HtmlDecode、HttpServerUtility.HtmlEncode的区别?

它们与下面一般手工写的代码有什么区别?

  1. public static string htmlencode(string str)
  2. {
  3. if (str == null || str == "")
  4. return "";
  5. str.Replace("<", "<");
  6. str.Replace(">", ">");
  7. str.Replace(" ", " ");
  8. str.Replace(" ", "  ");
  9. str.Replace("/"", """);
  10. str.Replace("/'", "'");
  11. str.Replace("/n", "<br/>");
  12. return str;
  13. }

答案:

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;

它们只不过是为了调用方便,进行了封装而已;

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

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. TestChar("<");   //小于号        替换为      <
  4. TestChar(">");   //大于号        替换为      >
  5. TestChar(" ");    //英文半角空格        替换为      不做替换;
  6. TestChar(" ");  //中文全角空格        替换为      不做替换;
  7. TestChar("&");   //&        替换为      &
  8. TestChar("/'");   //单引号        替换为      ';
  9. TestChar("/"");   //双引号        替换为      "
  10. TestChar("/r");   //回车        替换为      不做替换;
  11. TestChar("/n");   //回车        替换为      不做替换;
  12. TestChar("/r/n");   //回车        替换为      不做替换;
  13. }
  14. protected void TestChar(String str)
  15. {
  16. Response.Write(Server.HtmlEncode(str));
  17. Response.Write("----------------------");
  18. Response.Write(HttpUility.HtmlEncode(str));
  19. Response.Write("<br/>");
  20. }

所以手工的替换方法还是很有必要的,处理一些HtmlEncode不支持的替换。

  1. public static string htmlencode(string str)
  2. {
  3. str.Replace("<", "<");
  4. str.Replace(">", ">");
  5. str.Replace(" ", " ");
  6. str.Replace(" ", " ");
  7. str.Replace("/'", "'");
  8. str.Replace("/"", """);
  9. str.Replace("/n", "<br/>");
  10. }

使用Reflector 查看 HttpUttility.HtmlEncode 的实现,我们就可以看到,它只考虑的五种情况,空格,回车是没有处理的:

  1. public static unsafe void HtmlEncode(string value, TextWriter output)
  2. {
  3. if (value != null)
  4. {
  5. if (output == null)
  6. {
  7. throw new ArgumentNullException("output");
  8. }
  9. int num = IndexOfHtmlEncodingChars(value, 0);
  10. if (num == -1)
  11. {
  12. output.Write(value);
  13. }
  14. else
  15. {
  16. int num2 = value.Length - num;
  17. fixed (char* str = ((char*) value))
  18. {
  19. char* chPtr = str;
  20. char* chPtr2 = chPtr;
  21. while (num-- > 0)
  22. {
  23. chPtr2++;
  24. output.Write(chPtr2[0]);
  25. }
  26. while (num2-- > 0)
  27. {
  28. chPtr2++;
  29. char ch = chPtr2[0];
  30. if (ch <= '>')
  31. {
  32. switch (ch)
  33. {
  34. case '&':
  35. {
  36. output.Write("&");
  37. continue;
  38. }
  39. case '/'':
  40. {
  41. output.Write("'");
  42. continue;
  43. }
  44. case '"':
  45. {
  46. output.Write(""");
  47. continue;
  48. }
  49. case '<':
  50. {
  51. output.Write("<");
  52. continue;
  53. }
  54. case '>':
  55. {
  56. output.Write(">");
  57. continue;
  58. }
  59. }
  60. output.Write(ch);
  61. continue;
  62. }
  63. if ((ch >= '/x00a0') && (ch < 'ā'))
  64. {
  65. output.Write("&#");
  66. output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
  67. output.Write(';');
  68. }
  69. else
  70. {
  71. output.Write(ch);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }

二、JS中的编码和解码

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

(转)几种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. Android应用开发中关于this.context=context的理解

    在Android应用开发中,有的类里面需要声明一个Context的成员变量,然后还需要在该类的构造函数中加上this.context=context;这行代码.为什么要这么写呢?不写不行么? 先看下面 ...

  2. Nginx源码研究二:NGINX的事件处理概论

    NGINX作为服务端的应用程序,在客户端发出数据后,服务端在做着这样一些处理,数据先会经过网卡,网卡会和操作系统做交互,经过操作系统的协议栈处理,再和不同的应用程序交互. 在这里面涉及两个概念,一个是 ...

  3. NET MVC+EF6+Bootstrap

    开源:ASP.NET MVC+EF6+Bootstrap开发框架   前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿1 ...

  4. Linux_install mod_ssl openssl apache

    1.下载 mod_ssl 和 apache 登入http://www.modssl.org/source/,下载 mod_ssl-2.8.31-1.3.41.targz: 2.8.31是mod_ssl ...

  5. javaWeb实现使用邮箱邮件找回密码功能

    JSP+Jmail+JavaBean 发邮件(转)2010-08-23 18:052007年04月14日 14:32/* * SendMail.java * * Created on 2007年3月3 ...

  6. Android事件监听器Event Listener

    在 Android 中,我们可以通过事件处理使UI与用户互动(UI Events). UI的用户事件处理,即View处理用户的操作,在应用程序中几乎不可避免.View是重要的类,它是与用户互动的前线: ...

  7. LED驅動電路概述

    LED是一種固體光源,當它兩端加上正向電壓,半導體中的少數載流子和多數載流子發生復合,放出的過剩能量將引起光子發射.采用不同的材料,可制成不同顏色有 發光二極管 . 隨著對LED研究的進一步深入,其光 ...

  8. 51单片机模拟I2C总线的C语言实现

    电路原理图   EEPROM为ATMEL公司的AT24C01A.单片机为ATMEL公司的AT89C51. 软件说明 C语言为Franklin C V3.2.将源程序另存为testi2c.c,用命令 C ...

  9. MCS-51系列和80C51系列单片机是否相同

    MCS是Intel公司单片机的系列符号.Intel推出有MCS-48.MCS-51.MCS-96系列单片机. MCS-51系列单既包括三个基本型80C31.8051.8751,以及对应的低功耗型号80 ...

  10. B450配置

    电脑型号 联想 B450 笔记本电脑  (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...