如何进行 HTML 编码和解码?

Mister 2007-01-20 10:08:03
在非引用 System.Web.dll 情况下,即非 Web 应用程序下,有没有什么类能进行 HTML 编码和解码?

Server.HtmlEncode() ..... 之类的答案,请不要回帖!
...全文
2252 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxf654 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
  • 打赏
  • 举报
回复
.....................................................

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧