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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 网际浪人

.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的结果都是一样的。