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);url
HttpUtility.UrlDecode 方法:spa
將已經爲在 URL 中傳輸而編碼的字符串轉換爲解碼的字符串。.net
重載列表
將已經爲在 URL 中傳輸而編碼的字符串轉換爲解碼的字符串。
[C#] public static string UrlDecode(string);3d
使用指定的解碼對象將 URL 編碼的字節數組轉換爲已解碼的字符串。
[C#] public static string UrlDecode(byte[], Encoding);code
使用指定的編碼對象將 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);
須要注意的幾點:
一、HttpUtility.UrlEncode,HttpUtility.UrlDecode是靜態方法,而Server.UrlEncode,Server.UrlDecode是實例方法。
二、Server是HttpServerUtility類的實例,是System.Web.UI.Page的屬性。
三、用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
四、有時候可能別的系統傳遞過來的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便可。