Edit

Share via


HttpUtility.UrlEncode Method

Definition

Encodes a URL string. These method overloads can be used to encode the entire URL, including query-string values.

To encode or decode values outside of a web application, use the WebUtility class.

Overloads

UrlEncode(Byte[], Int32, Int32)

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(String, Encoding)

Encodes a URL string using the specified encoding object.

UrlEncode(Byte[])

Converts a byte array into an encoded URL string.

UrlEncode(String)

Encodes a URL string.

UrlEncode(Byte[], Int32, Int32)

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

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

Parameters

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.

Returns

An encoded string.

Remarks

The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 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.

To encode or decode values outside of a web application, use the WebUtility class.

See also

Applies to

UrlEncode(String, Encoding)

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

Encodes a URL string using the specified encoding object.

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

Parameters

str
String

The text to encode.

e
Encoding

The Encoding object that specifies the encoding scheme.

Returns

An encoded string.

Remarks

This method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 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.

To encode or decode values outside of a web application, use the WebUtility class.

See also

Applies to

UrlEncode(Byte[])

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

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

Parameters

bytes
Byte[]

The array of bytes to encode.

Returns

An encoded string.

Remarks

The UrlEncode method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 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.

To encode or decode values outside of a web application, use the WebUtility class.

See also

Applies to

UrlEncode(String)

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

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

Parameters

str
String

The text to encode.

Returns

An encoded string.

Remarks

The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream without encoding, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. 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.

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. 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.

The HttpUtility.UrlEncode method uses UTF-8 encoding by default. Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter.

UrlEncode is a convenient way to access the UrlEncode method at run time from an ASP.NET application. Internally, UrlEncode uses the UrlEncode method to encode strings.

To encode or decode values outside of a web application, use the WebUtility class.

See also

Applies to