HttpUtility.UrlEncode HttpUtility.UrlEncode HttpUtility.UrlEncode HttpUtility.UrlEncode Method

定義

將 URL 字串編碼。Encodes a URL string. 這些方法多載可以用來編碼整個 URL,包含查詢字串值。These method overloads can be used to encode the entire URL, including query-string values.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

多載

UrlEncode(String, Encoding) UrlEncode(String, Encoding) UrlEncode(String, Encoding) UrlEncode(String, Encoding)

使用指定的編碼物件,編碼 URL 字串。Encodes a URL string using the specified encoding object.

UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。Converts a byte array into a URL-encoded string, starting at the specified position in the array and continuing for the specified number of bytes.

UrlEncode(Byte[]) UrlEncode(Byte[]) UrlEncode(Byte[]) UrlEncode(Byte[])

將位元組陣列轉換成編碼的 URL 字串。Converts a byte array into an encoded URL string.

UrlEncode(String) UrlEncode(String) UrlEncode(String) UrlEncode(String)

將 URL 字串編碼。Encodes a URL string.

UrlEncode(String, Encoding) UrlEncode(String, Encoding) UrlEncode(String, Encoding) UrlEncode(String, Encoding)

使用指定的編碼物件,編碼 URL 字串。Encodes a URL string using the specified encoding object.

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

參數

str
String String String String

要編碼的文字。The text to encode.

e
Encoding Encoding Encoding Encoding

可指定編碼配置的 Encoding 物件。The Encoding object that specifies the encoding scheme.

傳回

編碼字串。An encoded string.

備註

這個方法可用來編碼整個 URL,包括查詢字串值。This method can be used to encode the entire URL, including query-string values. 如果空白和標點符號之類的字元傳遞到 HTTP 資料流,它們可能錯誤地解譯接收方。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL 編碼會將對等的字元實體; 不允許在 URL 中的字元轉換URL 解碼反轉的編碼方式。URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 例如,當字元<和 >,就會內嵌在 URL 中傳輸的文字區塊,在它們被編碼為 %3c 和 %3e。For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32) UrlEncode(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。Converts a byte array into a URL-encoded string, starting at the specified position in the array and continuing for the specified number of bytes.

public:
 static System::String ^ UrlEncode(cli::array <System::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[]

要編碼的位元組陣列。The array of bytes to encode.

offset
Int32 Int32 Int32 Int32

位元組陣列中要開始編碼的位置。The position in the byte array at which to begin encoding.

count
Int32 Int32 Int32 Int32

要編碼的位元組數。The number of bytes to encode.

傳回

編碼字串。An encoded string.

備註

UrlEncode(String)方法可以用來編碼整個 URL,包括查詢字串值。The UrlEncode(String) method can be used to encode the entire URL, including query-string values. 如果空白和標點符號之類的字元傳遞到 HTTP 資料流,它們可能錯誤地解譯接收方。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL 編碼會將對等的字元實體; 不允許在 URL 中的字元轉換URL 解碼反轉的編碼方式。URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 例如,當字元<和 >,就會內嵌在 URL 中傳輸的文字區塊,在它們被編碼為 %3c 和 %3e。For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

UrlEncode(Byte[]) UrlEncode(Byte[]) UrlEncode(Byte[]) UrlEncode(Byte[])

將位元組陣列轉換成編碼的 URL 字串。Converts a byte array into an encoded URL string.

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

參數

bytes
Byte[]

要編碼的位元組陣列。The array of bytes to encode.

傳回

編碼字串。An encoded string.

備註

UrlEncode方法可以用來編碼整個 URL,包括查詢字串值。The UrlEncode method can be used to encode the entire URL, including query-string values. 如果空白和標點符號之類的字元傳遞到 HTTP 資料流,它們可能錯誤地解譯接收方。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL 編碼會將對等的字元實體; 不允許在 URL 中的字元轉換URL 解碼反轉的編碼方式。URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 例如,當字元<和 >,就會內嵌在 URL 中傳輸的文字區塊,在它們被編碼為 %3c 和 %3e。For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

UrlEncode(String) UrlEncode(String) UrlEncode(String) UrlEncode(String)

將 URL 字串編碼。Encodes a URL string.

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

參數

str
String String String String

要編碼的文字。The text to encode.

傳回

編碼字串。An encoded string.

備註

UrlEncode(String)方法可以用來編碼整個 URL,包括查詢字串值。The UrlEncode(String) method can be used to encode the entire URL, including query-string values. 如果字元,例如空白與標點符號會傳入 HTTP 資料流,而不進行編碼,它們可能錯誤地解譯接收方。If characters such as blanks and punctuation are passed in an HTTP stream without encoding, they might be misinterpreted at the receiving end. URL 編碼會將對等的字元實體; 不允許在 URL 中的字元轉換URL 解碼反轉的編碼方式。URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 例如,當字元<和 >,就會內嵌在 URL 中傳輸的文字區塊,在它們被編碼為 %3c 和 %3e。For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

您可以將使用的 URL 編碼UrlEncode方法或UrlPathEncode方法。You can encode a URL using with the UrlEncode method or the UrlPathEncode method. 不過,這些方法會傳回不同的結果。However, the methods return different results. UrlEncode方法會將每個空格字元轉換成加號字元 (+)。The UrlEncode method converts each space character to a plus character (+). UrlPathEncode方法會將每個空格字元到字串"%20",表示十六進位標記法中的空間。The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. 使用UrlPathEncode方法 URL 的路徑部分編碼才能保證一致解碼的 URL,不論哪一個平台或瀏覽器時所執行的解碼。Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

HttpUtility.UrlEncode方法會使用 utf-8 編碼預設。The HttpUtility.UrlEncode method uses UTF-8 encoding by default. 因此,使用UrlEncode方法會提供與使用相同的結果UrlEncode方法並指定UTF8做為第二個參數。Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter.

UrlEncode 是便利的方式來存取UrlEncode從 ASP.NET 應用程式的執行階段的方法。UrlEncode is a convenient way to access the UrlEncode method at run time from an ASP.NET application. 就內部而言,UrlEncode使用UrlEncode編碼字串的方法。Internally, UrlEncode uses the UrlEncode method to encode strings.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

適用於