博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
几种HtmlEncode的区别(转)
阅读量:7205 次
发布时间:2019-06-29

本文共 5640 字,大约阅读时间需要 18 分钟。

一、C#中的编码

HttpUtility.HtmlDecode、HttpUtility.HtmlEncodeServer.HtmlDecode、Server.HtmlEncodeHttpServerUtility.HtmlDecodeHttpServerUtility.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", "
"); 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("
"); }
[c-sharp]

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

public static string htmlencode(string str)  {      str.Replace("<", "<");      str.Replace(">", ">");      str.Replace(" ", " ");      str.Replace(" ", " ");      str.Replace("/'", "'");      str.Replace("/"", """);      str.Replace("/n", "
"); }
[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, 0);          if (num == -1)          {              output.Write(value);          }          else          {              int num2 = value.Length - num;              fixed (char* str = ((char*) value))              {                  char* chPtr = str;                  char* chPtr2 = chPtr;                  while (num-- > 0)                  {                      chPtr2++;                      output.Write(chPtr2[0]);                  }                  while (num2-- > 0)                  {                      chPtr2++;                      char ch = chPtr2[0];                      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.   
  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.     例外字符:! * ( ) '  

原文:

转载于:https://www.cnblogs.com/MirageFox/p/4814164.html

你可能感兴趣的文章
Bzoj1076 [SCOI2008]奖励关
查看>>
JCo 指南
查看>>
git使用--pull代码时冲突
查看>>
1-3-1动态随屏幕变化而变化
查看>>
Reading papers_6(Pattern Recognition And Machine Learning一书,ing...)
查看>>
java mybatis 新增记录 与 insertSelective 保存问题
查看>>
cocos2d-x 搭建android开发环境记录
查看>>
列表与元组的区别
查看>>
关于野指针、空指针
查看>>
key_buffer_size设置注意事项
查看>>
C#对各种文件的操作-ini(2)
查看>>
JavaScript入门学习笔记(二)
查看>>
Project Euler 13 Large sum
查看>>
自动工作量资料档案库(AWR)报告的获得
查看>>
virtualBox中的centOS虚拟机硬盘扩容
查看>>
Android应用目录结构分析
查看>>
动画总结(UIView的动画)
查看>>
顶点着色器和片断着色器
查看>>
vc++实现禁用按钮
查看>>
flask with gae开发小结
查看>>