System.Web.HttpUtility VS System.Web.HttpServerUtility VS System.Net.WebUtility

时间:2024-01-07 21:49:26
HttpUtility 类作为 HttpServerUtility 类的内部使用,HttpServerUtility 通过System.Web.UI.Page.Server属性(WebForm)/Controller.Server(MVC)对外提供服务,他们都位于System.Web命名空间下,供asp.net使用
System.Net.WebUtility提供相似的API,用于非asp.net应用,API内部有细微差别,具体可以查看.net源码http://referencesource.microsoft.com
public sealed class HttpServerUtility {
public string HtmlEncode(string s) {
return HttpUtility.HtmlEncode(s);
} public void HtmlEncode(string s, TextWriter output) {
HttpUtility.HtmlEncode(s, output);
} public string HtmlDecode(string s) {
return HttpUtility.HtmlDecode(s);
} public void HtmlDecode(string s, TextWriter output) {
HttpUtility.HtmlDecode(s, output);
} public string UrlEncode(string s) {
Encoding e = (_context != null) ? _context.Response.ContentEncoding : Encoding.UTF8;
return HttpUtility.UrlEncode(s, e);
} public void UrlEncode(string s, TextWriter output) {
if (s != null)
output.Write(UrlEncode(s));
} public string UrlDecode(string s) {
Encoding e = (_context != null) ? _context.Request.ContentEncoding : Encoding.UTF8;
return HttpUtility.UrlDecode(s, e);
}
public void UrlDecode(string s, TextWriter output) {
if (s != null)
output.Write(UrlDecode(s));
}
}