Server.UrlEncode、HttpUtility.UrlDecode不同编码
生活随笔
收集整理的這篇文章主要介紹了
Server.UrlEncode、HttpUtility.UrlDecode不同编码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
同樣的Server.UrlEncode在不同頁面居然編碼后的字符不同,后來查了查原來.. 在對URL進行編碼時,該用哪一個?這兩都使用上有什么區(qū)別嗎?
測試:
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("原數(shù)據(jù):"+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);
輸出:
原數(shù)據(jù):文件上(傳)篇.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
區(qū)別在于:HttpUtility.UrlEncode()默認是以UTF8對URL進行編碼,而Server.UrlEncode()則以默認的編碼對URL進行編碼。
在用 ASP.Net 開發(fā)頁面的時候, 我們常常通過 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在頁面間通過 URL 傳遞參數(shù). 成對的使用 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"的.
經(jīng)檢查, 在 .Net 2.0 也是這樣.
上面是從別的地方拷貝的,寫得很好,我自己的一個程序中也遇到同樣的問題,默認aspx是以utf-8為編碼的,在我這個程序中必須用gb2312為默認編碼(<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>),問題出現(xiàn)了,以前沒有問題的HttpUtility.UrlDecode在Page.Request回的值是亂碼這就是上面說的HttpUtility.UrlDecode默認以UTF8對URL進行編碼,這種情況下面只需將HttpUtility.UrlDecode改成Server.UrlEncode即可。
測試:
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("原數(shù)據(jù):"+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);
輸出:
原數(shù)據(jù):文件上(傳)篇.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
區(qū)別在于:HttpUtility.UrlEncode()默認是以UTF8對URL進行編碼,而Server.UrlEncode()則以默認的編碼對URL進行編碼。
在用 ASP.Net 開發(fā)頁面的時候, 我們常常通過 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在頁面間通過 URL 傳遞參數(shù). 成對的使用 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"的.
經(jīng)檢查, 在 .Net 2.0 也是這樣.
上面是從別的地方拷貝的,寫得很好,我自己的一個程序中也遇到同樣的問題,默認aspx是以utf-8為編碼的,在我這個程序中必須用gb2312為默認編碼(<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>),問題出現(xiàn)了,以前沒有問題的HttpUtility.UrlDecode在Page.Request回的值是亂碼這就是上面說的HttpUtility.UrlDecode默認以UTF8對URL進行編碼,這種情況下面只需將HttpUtility.UrlDecode改成Server.UrlEncode即可。
轉載于:https://www.cnblogs.com/zwl12549/archive/2009/04/06/1430287.html
總結
以上是生活随笔為你收集整理的Server.UrlEncode、HttpUtility.UrlDecode不同编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】java string类的方法及说
- 下一篇: SQL中条件和比较关键字Case的使用方