C# .net 几种HtmlEncode,HtmlDecode的区别

18 篇文章 0 订阅
3 篇文章 0 订阅

 

一、C#中的编码

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

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

 

[c-sharp] view plaincopy

 

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

 

答案:

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;

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

 

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

 

[c-sharp] view plaincopy

 

  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不支持的替换。

 

[c-sharp] view plaincopy

 

  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 的实现,我们就可以看到,它只考虑的五种情况,空格,回车是没有处理的:

 

[c-sharp] view plaincopy

 

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

 

[c-sharp] view plaincopy

 

  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.     例外字符:! * ( ) '  

 

 

 

.NET编码解码(HtmlEncode与HtmlEncode)

编码代码:

System.Web.HttpUtility.HtmlEncode("<a href=\"http://hovertree.com/\">何问起</a>");

解码代码:

System.Web.HttpUtility.HtmlDecode("&lt;a href=&quot;http://hovertree.com/&quot;&gt;&#20309;&#38382;&#36215;&lt;/a&gt; "); 

效果体验:http://tool.hovertree.com/htmlcode/

本工具还可以实现Unicode解码,例如可以把以下代码解码试试:
 

&#60;&#97;&#32;&#104;&#114;&#101;&#102;&#61;&#34;&#104;&#116;&#116;&#112;&#58;&#47;&#47;&#104;&#111;&#118;&#101;&#114;&#116;&#114;&#101;&#101;&#46;&#99;&#111;&#109;&#47;&#34;&#62;&#20309;&#38382;&#36215;&#65292;&#25105;&#29233;&#20320;&#65292;&#23601;&#20687;&#32769;&#40736;&#29233;&#22823;&#31859;&#12290;&#60;&#47;&#97;&#62;&#32;

 

详细实现:


Default.aspx

复制代码

<%@ Page ValidateRequest="false" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>asp.net(C#) 编码解码(HtmlEncode与HtmlEncode)- 何问起</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtInput" runat="server" Height="194px" TextMode="MultiLine" Width="305px"></asp:TextBox>
<asp:Button ID="btnOk" runat="server" Text="提交" OnClick="btnOk_Click" /></div>
</form>
</body>
</html>

复制代码

Default.aspx.cs

复制代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/***********************编码研究***********************
* 1.默认情况是不允许用户在TextBox中输入html标签的,
* 如果需要输入,设置Page的ValidateRequest="false"
* 2.可以把输入的html标签,比如<input>直接存放在数据库中,
* 只是在输出的时候编码,防止原样输出打乱页面布局.或者呈现html元素.
*****************************************************/
public partial class test_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// http://sosoft.cnblogs.com/
}
protected void btnOk_Click(object sender, EventArgs e)
{
lblShow.Text = htmlEncode(txtInput.Text);
} 
/// <summary>
/// 对输入的html编码,同时对回车与空格进行转换
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string htmlEncode(string str)
{
return Server.HtmlEncode(str).Replace("\n", "<br/>").Replace(" ", " ");
}

}

复制代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值