關於HttpUtility.UrlEncode,HttpUtility.UrlDecode,Server.UrlEncode,Server.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);
例:
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%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%e5%a5%a5%e8%bf%b7%e5%b0%94
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb


原因:Server.UrlEncode的編碼方式是按照本地程序設置的編碼方式進行編碼的,而HttpUtility.UrlEncode是默認的按照.net的utf-8格式進行編碼的。

如果改一下程序:
string url1="http://search.99read.com/index.aspx?book_search=all&main_str=奧迷爾";
Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312")));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url1));

輸出的結果是:
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb

4、有時候可能別的系統傳遞過來的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();

 

在對URL進行編碼時,該用哪一個?這兩都使用上有什麼區別嗎?
測試:
string file="文件上(傳)篇.doc";
string Server_UrlEncode=Server.UrlEncode(file);
string Server_UrlDecode=Server.UrlDecode(Server_UrlEncode);
string HttpUtility_UrlEncode=System.Web.HttpUtility.UrlEncode(file);
string HttpUtility_UrlDecode=System.Web.HttpUtility.UrlDecode(HttpUtility_UrlEncode);
Response.Write("原數據:"+file);
SFun.WriteLine("Server.UrlEncode:"+Server_UrlEncode);
SFun.WriteLine("Server.UrlDecode:"+Server_UrlDecode);
SFun.WriteLine("HttpUtility.UrlEncode:"+HttpUtility_UrlEncode);
SFun.WriteLine("HttpUtility.UrlDecode:"+HttpUtility_UrlDecode);

輸出:
原數據:文件上(傳)篇.doc 
Server.UrlEncode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.UrlDecode:文件上(傳)篇.doc
HttpUtility.UrlEncode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc
HttpUtility.UrlDecode:文件上(傳)篇.doc

區別在於:HttpUtility.UrlEncode()默認是以UTF8對URL進行編碼,而Server.UrlEncode()則以默認的編碼對URL進行編碼。

在用 ASP.Net 開發頁面的時候, 我們常常通過 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在頁面間通過 URL 傳遞參數. 成對的使用 Encode 和 Decode 是沒有問題的.

但是, 我們在編寫文件下載的頁面的時候, 常常用如下方法來指定下載的文件的名稱:
Response.AddHeader("Content-Disposition","attachment; filename="
+ HttpUtility.UrlEncode(fileName, Encoding.UTF8));
之所以轉換成 UTF8 是爲了支持中文文件名.

這 時候問題就來了, 因爲 HttpUtility.UrlEncode 在 Encode 的時候, 將空格轉換成加號('+'), 在 Decode 的時候將加號轉爲空格, 但是瀏覽器是不能理解加號爲空格的, 所以如果文件名包含了空格, 在瀏覽器下載得到的文件, 空格就變成了加號.

一個解決辦法是, 在 HttpUtility 的 UrlEncode 之後, 將 "+" 替換成 "%20"( 如果原來是 "+" 則被轉換成 "%2b" ) , 如: 
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8); 
fileName = fileName.Replace("+", "%20"); 
不明白微軟爲什麼要把空格轉換成加號而不是"%20". 記得 JDK 的 UrlEncoder 是將空格轉換成 "%20"的.
經檢查, 在 .Net 2.0 也是這樣.

上面是從別的地方拷貝的,寫得很好,我自己的一個程序中也遇到同樣的問題,默認aspx是以utf-8爲編碼的,在我這個程序中必須用gb2312爲默認編碼(<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>),問題出現了,以前沒有問題的HttpUtility.UrlDecode在Page.Request回的值是亂碼這就是上面說的HttpUtility.UrlDecode默認以UTF8對URL進行編碼,這種情況下面只需將HttpUtility.UrlDecode改成Server.UrlEncode即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章