社区
C#
帖子详情
如何进行 HTML 编码和解码?
Mister
2007-01-20 10:08:03
在非引用 System.Web.dll 情况下,即非 Web 应用程序下,有没有什么类能进行 HTML 编码和解码?
Server.HtmlEncode() ..... 之类的答案,请不要回帖!
...全文
2255
11
打赏
收藏
如何进行 HTML 编码和解码?
在非引用 System.Web.dll 情况下,即非 Web 应用程序下,有没有什么类能进行 HTML 编码和解码? Server.HtmlEncode() ..... 之类的答案,请不要回帖!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
「已注销」
2007-02-12
打赏
举报
回复
up
flyin2006
2007-02-12
打赏
举报
回复
public static string HtmlDecode(string s)
{
if (s == null)
{
return null;
}
if (s.IndexOf('&') < 0)
{
return s;
}
StringBuilder builder1 = new StringBuilder();
StringWriter writer1 = new StringWriter(builder1);
HttpUtility.HtmlDecode(s, writer1);
return builder1.ToString();
}
public static void HtmlDecode(string s, TextWriter output)
{
if (s != null)
{
if (s.IndexOf('&') < 0)
{
output.Write(s);
}
else
{
int num1 = s.Length;
for (int num2 = 0; num2 < num1; num2++)
{
char ch1 = s[num2];
if (ch1 == '&')
{
int num3 = s.IndexOfAny(HttpUtility.s_entityEndingChars, num2 + 1);
if ((num3 > 0) && (s[num3] == ';'))
{
string text1 = s.Substring(num2 + 1, (num3 - num2) - 1);
if ((text1.Length > 1) && (text1[0] == '#'))
{
try
{
if ((text1[1] == 'x') || (text1[1] == 'X'))
{
ch1 = (char) ((ushort) int.Parse(text1.Substring(2), NumberStyles.AllowHexSpecifier));
}
else
{
ch1 = (char) ((ushort) int.Parse(text1.Substring(1)));
}
num2 = num3;
}
catch (FormatException)
{
num2++;
}
catch (ArgumentException)
{
num2++;
}
}
else
{
num2 = num3;
char ch2 = HtmlEntities.Lookup(text1);
if (ch2 != '\0')
{
ch1 = ch2;
}
else
{
output.Write('&');
output.Write(text1);
output.Write(';');
goto Label_0103;
}
}
}
}
output.Write(ch1);
Label_0103:;
}
}
}
}
flyin2006
2007-02-12
打赏
举报
回复
帮你反编译下:
public static string HtmlEncode(string s)
{
if (s == null)
{
return null;
}
int num1 = HttpUtility.IndexOfHtmlEncodingChars(s, 0);
if (num1 == -1)
{
return s;
}
StringBuilder builder1 = new StringBuilder(s.Length + 5);
int num2 = s.Length;
int num3 = 0;
Label_002A:
if (num1 > num3)
{
builder1.Append(s, num3, num1 - num3);
}
char ch1 = s[num1];
if (ch1 > '>')
{
builder1.Append("&#");
builder1.Append(ch1.ToString(NumberFormatInfo.InvariantInfo));
builder1.Append(';');
}
else
{
char ch2 = ch1;
if (ch2 != '"')
{
switch (ch2)
{
case '<':
builder1.Append("<");
goto Label_00D5;
case '=':
goto Label_00D5;
case '>':
builder1.Append(">");
goto Label_00D5;
case '&':
builder1.Append("&");
goto Label_00D5;
}
}
else
{
builder1.Append(""");
}
}
Label_00D5:
num3 = num1 + 1;
if (num3 < num2)
{
num1 = HttpUtility.IndexOfHtmlEncodingChars(s, num3);
if (num1 != -1)
{
goto Label_002A;
}
builder1.Append(s, num3, num2 - num3);
}
return builder1.ToString();
}
flyin2006
2007-02-12
打赏
举报
回复
我教你一招,用Reflector反编译System.Web.dll,看看HtmlEncode怎么实现的
或者直接用其代码 ;-)
james_hunter
2007-02-12
打赏
举报
回复
偏要用Server.HtmlEncode() ..... 之类的答案回复!
咬我!
he_8134
2007-02-12
打赏
举报
回复
同意楼上...
winner2050
2007-02-12
打赏
举报
回复
不是web程序也能用System.Web.dll
何必要虐待自己呢.
tgl10
2007-02-12
打赏
举报
回复
自己看下编码算法吧,不是很难
Mister
2007-02-12
打赏
举报
回复
来个人结贴,谢谢。
Mister
2007-01-22
打赏
举报
回复
来人啊~~~
Mister
2007-01-21
打赏
举报
回复
.....................................................
如何将
html
内容
解码
,3.5.3 对
HTML
进行
编码
和
解码
3.5.3 对
HTML
进行
编码
和
解码
由于
HTML
是一种由符号标记的语言,...这些方法就是
HTML
编码
。在
进行
HTML
文本处理的时候,通常要对
HTML
文本
进行
编码
和
解码
的操作。在ASP.NET中,Server对象提供了
Html
Encode和
Html
Decode...
Html
编码
与
解码
HTML
编码
与
解码
HTML
是一种标记语言,其中<、>等符号有着特殊的含义,当我们需要在网页上显示的文本中有含有这些标记的时候,就会造成浏览器错误的处理。 2例如,我们希望显示在网页中的文本内容是<h1>...
Python 中
html
模块:
HTML
编码
与
解码
的实用指南
html
模块主要用于
HTML
数据的
编码
和
解码
。在
HTML
中,某些字符具有特殊含义,如等,如果直接在
HTML
文档中使用这些字符,可能会导致解析错误。因此,需要将这些特殊字符转换为
HTML
实体引用,这就是
HTML
编码
的...
jquery
html
解码
字符串,jquery如何
编码
和
解码
url?
jquery如何
编码
和
解码
url?...在jQuery中,我们可以使用以下方法实现URL的
编码
和
解码
:encodeURIComponent(url) :可把字符串作为 URI 组件
进行
编码
。decodeURIComponent(url):可对 encodeURIComponen...
一文搞懂字符
编码
与
解码
最近遇到一个关于字符
编码
与
解码
的问题,使用GB2312保存了一个文件,然后使用vscode打开的时候,发现中文字符全是乱码了。为什么会出现这个问题?研究了一下
编码
与
解码
。 文件在计算机上存储的都是二进制。...
C#
111,097
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章