HttpUtility.UrlEncode 方法:
对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。
重载列表将字节数组转换为已编码的 URL 字符串,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。[C#] public static string UrlEncode(byte[]);
对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。[C#] public static string UrlEncode(string);
使用指定的编码对象对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠 HTTP 传输。[C#] public static string UrlEncode(string, Encoding);
从数组中的指定位置开始一直到指定的字节数为止,将字节数组转换为 URL 编码的字符串,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输。[C#] public static string UrlEncode(byte[], int, int);
HttpUtility.UrlDecode 方法:
将已经为在 URL 中传输而编码的字符串转换为解码的字符串。
重载列表将已经为在 URL 中传输而编码的字符串转换为解码的字符串。[C#] public static string UrlDecode(string);
使用指定的解码对象将 URL 编码的字节数组转换为已解码的字符串。[C#] public static string UrlDecode(byte[], Encoding);
使用指定的编码对象将 URL 编码的字符串转换为已解码的字符串。[C#] public static string UrlDecode(string, Encoding);
使用指定的编码对象,从数组中的指定位置开始到指定的字节数为止,将 URL 编码的字节数组转换为已解码的字符串。[C#] public static string UrlDecode(byte[], int, int, Encoding);
Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。HttpServerUtility.UrlEncode 方法:编码字符串,以便通过 URL 从 Web 服务器到客户端进行可靠的 HTTP 传输。
重载列表对字符串进行 URL 编码,并返回已编码的字符串。[C#] public string UrlEncode(string);
URL 对字符串进行编码,并将结果输出发送到 TextWriter 输出流。[C#] public void UrlEncode(string, TextWriter);例:String TestString = "This is a <Test String>.";StringWriter writer = new StringWriter();Server.UrlEncode(TestString, writer);String EncodedString = writer.ToString();
HttpServerUtility.UrlDecode 方法:对字符串进行解码,该字符串为了进行 HTTP 传输而进行编码并在 URL 中发送到服务器。
重载列表对字符串进行 URL 解码并返回已解码的字符串。[C#] public string UrlDecode(string);
对在 URL 中接收的 HTML 字符串进行解码,并将结果输出发送到 TextWriter 输出流。[C#] public void UrlDecode(string, TextWriter);
需要注意的几点:1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。2、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。3、用HttpUtility.UrlEncode编码后的字符串和用Server.UrlEncode进行编码后的字符串对象不一样:例如:string url="http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔";Response.Write(HttpUtility.UrlEncode(url));Response.Write("<br>");Response.Write(Server.UrlEncode(url));
输出结果是:http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔http://search.99read.com/index.aspx?book_search=all&main_str=
