HttpUtility.UrlEncode 方法

定义

对 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)

使用指定的编码对象,对 URL 字符串进行编码。Encodes a URL string using the specified encoding object.

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[])

将字节数组转换为编码的 URL 字符串。Converts a byte array into an encoded URL string.

UrlEncode(String)

对 URL 字符串进行编码。Encodes a URL string.

UrlEncode(String, Encoding)

使用指定的编码对象,对 URL 字符串进行编码。Encodes a URL string using the specified encoding object.

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

参数

strs
String

要编码的文本。The text to encode.

eEnc
Encoding

指定编码方案的 Encoding 对象。The Encoding object that specifies the encoding scheme.

返回

String

已编码的字符串。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. 例如,当字符 < and > 嵌入到要在 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)

从数组中指定位置开始,按照指定字节数量,将字节数组转换为 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);
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

从其开始编码的字节数组中的位置。The position in the byte array at which to begin encoding.

count
Int32

要编码的字节数。The number of bytes to encode.

返回

String

已编码的字符串。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. 例如,当字符 < and > 嵌入到要在 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[])

将字节数组转换为编码的 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);
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.

返回

String

已编码的字符串。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. 例如,当字符 < and > 嵌入到要在 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)

对 URL 字符串进行编码。Encodes a URL string.

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

要编码的文本。The text to encode.

返回

String

已编码的字符串。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. 例如,当字符 < and > 嵌入到要在 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 UrlPathEncodeYou 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.

UrlEncodeUrlEncode 在运行时从 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.

另请参阅

适用于