日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#编码、解码

發布時間:2025/1/21 C# 79 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#编码、解码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、HttpUtility.UrlEncode 方法:

對 URL 字符串進行編碼,以便實現從 Web 服務器到客戶端的可靠的 HTTP 傳輸。重載列表:

[1]將字節數組轉換為已編碼的 URL 字符。 [C#] public static string UrlEncode(byte[]);

[2]對 URL 字符串進行編碼。 [C#] public static string UrlEncode(string);

[3]使用指定的編碼對象對 URL 字符串進行編碼。 [C#] public static string UrlEncode(string, Encoding);

[4]從數組中的指定位置開始一直到指定的字節數為止,將字節數組轉換為 URL 編碼的字符串。 [C#] public static string UrlEncode(byte[], int, int);

2、HttpUtility.UrlDecode 方法:

將已經為在 URL 中傳輸而編碼的字符串轉換為解碼的字符串。重載列表:

[1][C#] public static string UrlDecode(string);

[2]使用指定的解碼對象將 URL 編碼的字節數組轉換為已解碼的字符串。 [C#] public static string UrlDecode(byte[], Encoding);

[3]使用指定的編碼對象將 URL 編碼的字符串轉換為已解碼的字符串。 [C#] public static string UrlDecode(string, Encoding);

[4]使用指定的編碼對象,從數組中的指定位置開始到指定的字節數為止,將 URL 編碼的字節數組轉換為已解碼的字符串。

[C#] public static string UrlDecode(byte[], int, int, Encoding);

3、Server是HttpServerUtility類的實例,是System.Web.UI.Page的屬性。

HttpServerUtility.UrlEncode 方法:

編碼字符串,以便通過 URL 從 Web 服務器到客戶端進行可靠的 HTTP 傳輸。重載列表:

[1]對字符串進行 URL 編碼,并返回已編碼的字符串。 [C#] public string UrlEncode(string);

[2]URL 對字符串進行編碼,并將結果輸出發送到 TextWriter 輸出流。 [C#] public void UrlEncode(string, TextWriter);

例: String str= "中國";

?????? StringWriter writer = new StringWriter();

?????? Server.UrlEncode(str, writer);

?????? String EncodedString = writer.ToString();

4、HttpServerUtility.UrlDecode 方法:

對字符串進行解碼,該字符串為了進行 HTTP 傳輸而進行編碼并在 URL 中發送到服務器。重載列表 :

[1]對字符串進行 URL 解碼并返回已解碼的字符串。 [C#] public string UrlDecode(string);

[2]對在 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://localhost:4349/name=中國";

???????? Response.Write(HttpUtility.UrlEncode(url));

???????? Response.Write("<br>");

???????? Response.Write(Server.UrlEncode(url));

輸出結果是:

http%3a%2f%2flocalhost%3a4349%2fname%3d%e4%b8%ad%e5%9b%bd

http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd

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

如果改一下程序: string url="http://localhost:4349/name=中國";

Response.Write(HttpUtility.UrlEncode(url,System.Text.Encoding.GetEncoding("GB2312")));

Response.Write("<br>");

Response.Write(Server.UrlEncode(url));

輸出的結果是:

http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd

http%253a%252f%252flocalhost%253a4349%252fname%253d%25e4%25b8%25ad%25e5%259b%25bd

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就是你提交的參數的Keyreturn 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 也是這樣.

總結

以上是生活随笔為你收集整理的C#编码、解码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。