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。

可以使用 和 UrlEncode 方法或 UrlPathEncode 方法对 URL 进行编码。 但是,这些方法返回不同的结果。 方法 UrlEncode 将每个空格字符转换为加号字符 (+) 。 方法 UrlPathEncode 将每个空格字符转换为字符串“%20”,该字符串以十六进制表示法表示一个空格。 在对 URL 的路径部分进行编码时使用 UrlPathEncode 方法,以确保解码的 URL 一致,而不考虑执行解码的平台或浏览器。

默认情况下, HttpUtility.UrlEncode 方法使用 UTF-8 编码。 因此,使用 UrlEncode 方法提供的结果与使用 UrlEncode 方法相同,并将 指定 UTF8 为第二个参数。

UrlEncode 是在运行时从 ASP.NET 应用程序访问 UrlEncode 方法的便捷方法。 在内部, UrlEncode 使用 UrlEncode 方法对字符串进行编码。

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。

另请参阅

适用于