關於HttpUtility.UrlEncode,HttpUtility.UrlDecode,Server.UrlEncode,Server.UrlDecode

來源:互聯網
上載者:User

 

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即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.