HttpUtility.UrlEncode 方法

定義

將 URL 字串編碼。 這些方法多載可以用來編碼整個 URL,包含查詢字串值。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

多載

UrlEncode(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。

UrlEncode(String, Encoding)

使用指定的編碼物件,編碼 URL 字串。

UrlEncode(Byte[])

將位元組陣列轉換成編碼的 URL 字串。

UrlEncode(String)

將 URL 字串編碼。

UrlEncode(Byte[], Int32, Int32)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。

public:
 static System::String ^ UrlEncode(cli::array <System::Byte> ^ bytes, int offset, int count);
public static string? UrlEncode (byte[]? bytes, int offset, int count);
public static string UrlEncode (byte[] bytes, int offset, int count);
static member UrlEncode : byte[] * int * int -> string
Public Shared Function UrlEncode (bytes As Byte(), offset As Integer, count As Integer) As String

參數

bytes
Byte[]

要編碼的位元組陣列。

offset
Int32

位元組陣列中要開始編碼的位置。

count
Int32

要編碼的位元組數。

傳回

編碼字串。

備註

方法 UrlEncode(String) 可用來編碼整個 URL,包括查詢字串值。 如果在 HTTP 數據流中傳遞空白和標點符號之類的字元,在接收端可能會誤譯這些字元。 URL 編碼會將URL中不允許的字元轉換成字元實體對等專案;URL 譯碼會反轉編碼。 例如,當字元 < 和 > 內嵌在要以URL傳輸的文字區塊時,它們會編碼為 %3c 和 %3e。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於

UrlEncode(String, Encoding)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

使用指定的編碼物件,編碼 URL 字串。

public:
 static System::String ^ UrlEncode(System::String ^ str, System::Text::Encoding ^ e);
public static string? UrlEncode (string? str, System.Text.Encoding e);
public static string UrlEncode (string str, System.Text.Encoding e);
static member UrlEncode : string * System.Text.Encoding -> string
Public Shared Function UrlEncode (str As String, e As Encoding) As String

參數

str
String

要編碼的文字。

e
Encoding

可指定編碼配置的 Encoding 物件。

傳回

編碼字串。

備註

這個方法可用來編碼整個 URL,包括查詢字串值。 如果在 HTTP 數據流中傳遞空白和標點符號之類的字元,在接收端可能會誤譯這些字元。 URL 編碼會將URL中不允許的字元轉換成字元實體對等專案;URL 譯碼會反轉編碼。 例如,當字元 < 和 > 內嵌在要以URL傳輸的文字區塊時,它們會編碼為 %3c 和 %3e。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於

UrlEncode(Byte[])

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

將位元組陣列轉換成編碼的 URL 字串。

public:
 static System::String ^ UrlEncode(cli::array <System::Byte> ^ bytes);
public static string? UrlEncode (byte[]? bytes);
public static string UrlEncode (byte[] bytes);
static member UrlEncode : byte[] -> string
Public Shared Function UrlEncode (bytes As Byte()) As String

參數

bytes
Byte[]

要編碼的位元組陣列。

傳回

編碼字串。

備註

方法 UrlEncode 可用來編碼整個 URL,包括查詢字串值。 如果在 HTTP 數據流中傳遞空白和標點符號之類的字元,在接收端可能會誤譯這些字元。 URL 編碼會將URL中不允許的字元轉換成字元實體對等專案;URL 譯碼會反轉編碼。 例如,當字元 < 和 > 內嵌在要以URL傳輸的文字區塊時,它們會編碼為 %3c 和 %3e。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於

UrlEncode(String)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

將 URL 字串編碼。

public:
 static System::String ^ UrlEncode(System::String ^ str);
public static string? UrlEncode (string? str);
public static string UrlEncode (string str);
static member UrlEncode : string -> string
Public Shared Function UrlEncode (str As String) As String

參數

str
String

要編碼的文字。

傳回

編碼字串。

備註

方法 UrlEncode(String) 可用來編碼整個 URL,包括查詢字串值。 如果空白和標點符號之類的字元在 HTTP 數據流中傳遞而不進行編碼,在接收端可能會誤譯。 URL 編碼會將URL中不允許的字元轉換成字元實體對等專案;URL 譯碼會反轉編碼。 例如,當字元 < 和 > 內嵌在要以URL傳輸的文字區塊時,它們會編碼為 %3c 和 %3e。

您可以使用 方法 UrlEncodeUrlPathEncode 方法來編碼 URL。 不過,方法會傳回不同的結果。 方法 UrlEncode 會將每個空格元轉換成加號字元 (+) 。 方法 UrlPathEncode 會將每個空格元轉換成字串 「%20」 代表十六進位表示法中的空格。 UrlPathEncode當您編碼 URL 的路徑部分以確保一致的譯碼 URL 時,請使用 方法,不論哪個平臺或瀏覽器執行譯碼。

方法 HttpUtility.UrlEncode 預設會使用UTF-8編碼。 因此,使用 UrlEncode 方法提供與使用 UrlEncode 方法相同的結果,並將 指定 UTF8 為第二個參數。

UrlEncode 是一種方便的方式,可從 ASP.NET 應用程式在運行時間存取 UrlEncode 方法。 在內部, UrlEncode 使用 UrlEncode 方法來編碼字串。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於