關於HttpUtility.UrlEncode、UrlDecode,Server.UrlEncode、UrlDecode編碼
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);
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://www.xingzhu.net.cn?name=客戶管理";
Response.Write(HttpUtility.UrlEncode(url));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url));
輸出結果是:
http%3a%2f%2fwww.xingzhu.net.cn%3fname%3d%e5%ae%a2%e6%88%b7%e7%ae%a1%e7%90%86
http%3a%2f%2fwww.xingzhu.net.cn%3fname%3d%e5%ae%a2%e6%88%b7%e7%ae%a1%e7%90%86
原因:Server.UrlEncode的編碼方式是按照本地程式設定的編碼方式進行編碼的,而HttpUtility.UrlEncode是預設的按照.net的utf-8格式進行編碼的。
如果改一下程式:
string url1="http://www.xingzhu.net.cn/Search.aspx?type=news&tags=製造業";
Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312")));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url1));
輸出的結果是:
http%3a%2f%2fwww.xingzhu.net.cn%2fSearch.aspx%3ftype%3dnews%26tags%3d%d6%c6%d4%ec%d2%b5
http%3a%2f%2fwww.xingzhu.net.cn%2fSearch.aspx%3ftype%3dnews%26tags%3d%e5%88%b6%e9%80%a0%e4%b8%9a
3、有時候可能別的系統傳遞過來的url是用別的編碼方式編碼的。
介紹自己編寫的一個方法,可以擷取指定編碼格式的QueryString。
public string GetNonNullQueryString(string key,Encoding encoding)
{
//引用System.Collections.Specialized和System.Text命名空間
string stringValue;
System.Collections.Specialized.NameValueCollection encodingQueryString;
//該方法是在2.0中新增的
encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding);
//'裡面的key就是你提交的參數的Key
return encodingQueryString[key] != null ? encodingQueryString[key].Trim() : "";
}
調用:
string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();