惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
Help Net Security
Help Net Security
月光博客
月光博客
N
News and Events Feed by Topic
Cloudbric
Cloudbric
博客园 - 司徒正美
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
The Register - Security
The Register - Security
The Hacker News
The Hacker News
I
InfoQ
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
F
Fortinet All Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
C
Cisco Blogs
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Latest news
Latest news
PCI Perspectives
PCI Perspectives
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
T
Tor Project blog
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Webroot Blog
Webroot Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 网际浪人

.Net Framework 4.0 中利用Task实现并行处理、串并行混合处理 C# Process调用应用程序失败时应注意的问题 程序员的幽默 To腾讯:强行收集用户个人隐私的行为不可饶恕 VS2005打开VS2008项目的2种方法(转) 【转】Oracle Conversion Functions 晨星、银河基金业绩排行榜数据转换工具 ORACLE纯SQL实现多行合并一行 ASP.NET项目添加Log4Net后,发布后无法写日志 “必应”不应、“谷歌”不歌 你好2009,再见2008,牛年犇犇犇 一种在SQLServer中实现Sequence的高效方法 oledb使用Access更新和插入操作的注意点 5.12大地震——小学二年级小表妹谢可欣的诗 GridView自动排序 [转]SQL Server 2005链接字符串 GridView中使用DataKeyNames存储数据键值 C#调用Excel VBA宏 封装SoapException处理Webservice异常
对HtmlEncode的增强——HtmlEntitiesEncode
网际浪人 · 2008-04-24 · via 博客园 - 网际浪人

工作中我们常常需要加壳(escape)后传输或保存HTML文本,UI层使用时再进行脱壳(unescape)。
很庆幸.net为我们提供了非常好用的HttpUtility类,加壳时可采用HtmlEncode方法,脱壳时采用HtmlDecode。
但实际使用这两个方法时,即可知道HtmlDecode很好很实用;HtmlEncode却显得先天不足,仅能转换很少的一些html标记(如:<),
中文、全角符号或大量的特殊字符根本没有进行转换。因此在传输或保存入库时总会遇到这样那样的问题。

解决这一问题的方法其实很简单,每一个可敲出的字符总是对应了一个Unicode编码,而编码又对应了一个32位的整形数字,
那么使用 &#{数字}; 的形式就可以转换文本,即HtmlEntities
[关于HtmlEntities可查看:http://www.cnblogs.com/templates/waxdoll/htmlentities.htm]

转换的函数:

 1    /// <summary>
 2    /// HTMLEntitiesEncode(HTMLEntities编码)
 3    /// </summary>
 4    /// <param name="text">需要转换的html文本</param>
 5    /// <returns>HTMLEntities编码后的文本</returns>

 6    public static string HtmlEntitiesEncode(string text)
 7    {
 8        // 获取文本字符数组
 9        char[] chars = HttpUtility.HtmlEncode(text).ToCharArray();
10        
11        // 初始化输出结果
12        StringBuilder result = new StringBuilder(text.Length + (int)(text.Length * 0.1));
13
14        foreach (char c in chars)
15        {
16            // 将指定的 Unicode 字符的值转换为等效的 32 位有符号整数
17            int value = Convert.ToInt32(c);
18
19            // 内码为127以下的字符为标准ASCII编码,不需要转换,否则做 &#{数字}; 方式转换
20            if (value > 127)
21            {
22                result.AppendFormat("&#{0};", value);
23            }

24            else
25            {
26                result.Append(c);
27            }

28        }

29
30        return result.ToString();
31    }

(使用时记得引入 using System.Text

测试代码:

 1    protected void Button3_Click(object sender, EventArgs e)
 2    {
 3        // 常规HtmlEncode编码
 4        string strHtmlEncode = HttpUtility.HtmlEncode("<tr><td>《sss123┏你好啊┓123®はい》</td></tr>");
 5
 6        // 增强的HtmlEntities编码
 7        string strHtmlEntitiesEncode = HtmlEntitiesEncode("<tr><td>《sss123┏你好啊┓123®はい》</td></tr>");
 8
 9        string strHtmlDecode1 = HttpUtility.HtmlDecode(strHtmlEncode);
10
11        string strHtmlDecode2 = HttpUtility.HtmlDecode(strHtmlEntitiesEncode);
12    }

运行时监视截图:

可以很清楚的看到增强的HtmlEncode方法较完美地转换了中文或一些特殊字符,而且HtmlDecode的结果都是一样的。