C# URL 編碼

王林
發布: 2024-09-03 15:03:38
原創
685 人瀏覽過

字串 URL 可以使用 C# 中稱為 URL 編碼方法的方法進行編碼。可以使用此方法重載對整個URL 進行編碼,其中還包括字串的查詢值,如果我們想要對Web 應用程式之外的值進行解碼和編碼,可以使用稱為Web 實用程式類別的類別以及標點符號等特殊字符,傳遞到HTTP 流時將其留空。它有可能被誤解,但透過使用編碼,URL 中不允許的字元將被轉換為字元實體。完成的編碼可以使用 URL 解碼來反轉。

C# 中 URL Encode 的語法如下:

public static string UrlEncode (string strname, System.Text.Encoding e);
登入後複製

其中 strname 是必須編碼的文本,

e 是編碼對象,用於指定編碼方案。

使用 C# 進行 URL 編碼

  • 每當需要對字串 URL 進行編碼時,我們都會使用 C# 中名為 URL Encode 的方法。
  • 可以使用此方法重載對整個 URL 進行編碼,其中還包括字串的查詢值。
  • 使用關鍵字 read-only 定義的唯讀欄位的評估是在執行時完成的。
  • 如果我們想要對 Web 應用程式以外的值進行解碼和編碼,可以使用稱為 Web 實用程式類別的類別。
  • 標點符號、空白等特殊字元傳遞到HTTP流中時,有可能會被誤解,但透過編碼,URL中不允許的字元將被轉換為字元實體。
  • 完成的編碼可以使用 URL 解碼來反轉。

C# URL 編碼範例

下面提到了不同的例子:

範例#1

C# 程式示範 URL Encode 對給定 URL 進行編碼:

代碼:

using System.IO;
using System;
using System.Web;
//a class called program is defined
public class Program
{
//main method is called within which a string variable is defined to store the string
public static void Main()
{
//a variable called stringname is defined to store the URL which is to be encoded
string stringname = "https://www.educba.com/";
//UrlEncode method is used to encode the given URL
Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname));                }
}
登入後複製

輸出:

C# URL 編碼

在上面的程式中,定義了一個名為program的類別。然後呼叫main方法,其中定義了一個字串變數來儲存字串。然後定義一個名為 stringname 的變數來儲存要編碼的 URL。例如,該程式中要編碼的URL是https://www.educba.com/。然後使用 UrlEncode 方法對給定的 URL 進行編碼。最後,程式的輸出如上面的快照所示。

範例#2

C# 程式示範 URL Encode 對給定 URL 進行編碼。

代碼:

using System.IO;
using System;
using System.Web;
//a class called program is defined
public class Program
{
//main method is called within which a string variable is defined to store the string
public static void Main()
{
//a variable called stringname is defined to store the URL which is to be encoded
string stringname = "https://www.facebook.com/";
//UrlEncode method is used to encode the given URL
Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname));                }
}
登入後複製

輸出:

C# URL 編碼

在上面的程式中,定義了一個名為program的類別。然後呼叫main方法,其中定義了一個字串變數來儲存字串。然後定義一個名為 stringname 的變數來儲存要編碼的 URL。例如,程式中要編碼的URL是https://www.facebook.com/。然後使用 UrlEncode 方法對給定的 URL 進行編碼。最後,程式的輸出如上面的快照所示。

範例#3

C# 程式示範 URL Encode 對給定 URL 進行編碼。

代碼:

using System.IO;
using System;
using System.Web;
//a class called program is defined
public class Program
{
//main method is called within which a string variable is defined to store the string
public static void Main()
{
//a variable called stringname is defined to store the URL which is to be encoded
string stringname = "https://www.hotstar.com/in";
//UrlEncode method is used to encode the given URL
Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname));                }
}
登入後複製

輸出:

C# URL 編碼

在上面的程式中,定義了一個名為program的類別。然後呼叫main方法,其中定義了一個字串變數來儲存字串。然後定義一個名為 stringname 的變數來儲存要編碼的 URL。例如,程式中要編碼的URL是https://www.hotstar.com/in。然後使用 UrlEncode 方法對給定的 URL 進行編碼。最後,程式的輸出如上面的快照所示。

範例#4

C# 程式示範 URL Encode 對給定 URL 進行編碼。

代碼:

using System.IO;
using System;
using System.Web;
//a class called program is defined
public class Program
{
//main method is called within which a string variable is defined to store the string
public static void Main()
{
//a variable called stringname is defined to store the URL which is to be encoded
string stringname = " https://mail.google.com/mail/u/0/";
//UrlEncode method is used to encode the given URL
Console.WriteLine("The URL after encoding the given URL is: " + System.Web.HttpUtility.UrlEncode(stringname));                }
}
登入後複製

輸出:

C# URL 編碼

在上面的程式中,定義了一個名為program的類別。然後呼叫main方法,其中定義了一個字串變數來儲存字串。然後定義一個名為 stringname 的變數來儲存要編碼的 URL。例如,該程式中要編碼的URL是https://mail.google.com/mail/u/0/。然後使用 UrlEncode 方法對給定的 URL 進行編碼。最後,程式的輸出如上面的快照所示。

結論

在本教程中,我們透過定義來了解 C# 中 URL 編碼的概念、URL 編碼的語法,以及透過程式設計範例及其輸出來了解 C# 中 URL 編碼的工作原理。

以上是C# URL 編碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!