一、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, 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. 二、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

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

几种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. C# .net 几种HtmlEncode,HtmlDecode的区别

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

  4. ASP.NET Get和Post两种提交的区别

    表单form的提交有两种方式,一种是get的方法,一种是post 的方法.看下面代码,理解ASP.NET Get和Post两种提交的区别: < form id="form1" ...

  5. vue 路由传参 params 与 query两种方式的区别(转载)

    vue 路由传参 params 与 query两种方式的区别 初学vue的时候,不知道如何在方法中跳转界面并传参,百度过后,了解到两种方式,params 与 query.然后,错误就这么来了:  ro ...

  6. ASCII,unicode, utf8 ,big5 ,gb2312,gbk,gb18030等几种常用编码区别

    ASCII,unicode, utf8 ,big5 ,gb2312,gbk,gb18030等几种常用编码区别 最近老为编码问题而烦燥,下定决心一定要将其弄明白!本文主要总 结网上一些朋友提供的 asc ...

  7. axios请求接口http_使用axios请求接口,几种content-type的区别详解

    axios的使用 安装(一般使用框架的话, 脚手架都集成了) $ npm install axios 请求示例 // POST axios.post('/user', { firstName: 'Fr ...

  8. Java中的string定义的两种方法和区别

    java中的String定义的两种方法和区别 第一种:new方式 String s1 = new String("hello world"); String s2 = new St ...

  9. controller 有两种写法,讨论一下两种写法的区别:

    controller 有两种写法,讨论一下两种写法的区别: 写法 1: app.controller('myCtrl', function($scope, $location) { $scope.my ...

最新文章

  1. nodejs ajax进度条,Ajax异步文件上传与NodeJS express服务端处理的示例分析
  2. java面试解决项目难题_Java转换难题者,不适合工作(或面试)
  3. 07.敏捷项目管理——推测阶段笔记
  4. 有缘网分布式爬虫案例
  5. 65 年来,全英国向他道歉三次,图灵,计算机人不能忘记的男人
  6. 惠普m154a状态页_惠普新品NS—1005w无线智能应用与驱动安装篇
  7. Linux系统下卸载jdk的步骤
  8. ssh登录工具 putty 和 生成.ppk文件的puttygen工具 如何使用puttygen生成密钥
  9. 运维第二篇:Docker--Harbor私有镜像仓库搭建
  10. 使用matlab生成高斯滤波模板_matlab 高斯滤波(原创)
  11. c语言的关键字母大小写表示,英语26个字母大小写标准写法
  12. 关于win10专业版无法下载暴雪战网的解决方案
  13. 春招面试阿里,面试官让我说说Java8的新特性
  14. echarts上加横线标线_Echarts地图添加引导线效果(labelLine)
  15. 【教程】如果公司的网络屏蔽了游戏【英雄联盟】的链接请求,使用这种方法玩游戏。...
  16. vim 常用功能大全
  17. 根据ParentId生成树状结构这po事
  18. HDU 6617 Enveloping Convex(凸包+半平面交+二分)
  19. flowable实战(二)flowable流程模型管理接口
  20. Java JDK 动态代理实现和代码分析

热门文章

  1. 三维重建12:室内三维物体的位姿识别论文列表
  2. 获取验证码canvas
  3. 数据之路 Day5 - Python基础5
  4. C的function call與stack frame心得
  5. linux常用命令(用户篇)
  6. Phonegap + JqueryMobile常见问题
  7. 眼压与角膜厚度的关系
  8. AIO+BIO+NIO+同步+异步+阻塞+非阻塞
  9. 把hive数据导出至mysql
  10. OpenCV学习笔记九-Canny边缘检测