HttpUtility.UrlEncodeToBytes Method

Definition

Converts a string or a byte array into an encoded array of bytes.

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

Overloads

UrlEncodeToBytes(Byte[])

Converts an array of bytes into a URL-encoded array of bytes.

UrlEncodeToBytes(String)

Converts a string into a URL-encoded array of bytes.

UrlEncodeToBytes(String, Encoding)

Converts a string into a URL-encoded array of bytes using the specified encoding object.

UrlEncodeToBytes(Byte[], Int32, Int32)

Converts an array of bytes into a URL-encoded array of bytes, starting at the specified position in the array and continuing for the specified number of bytes.

UrlEncodeToBytes(Byte[])

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

Converts an array of bytes into a URL-encoded array of bytes.

C#
public static byte[]? UrlEncodeToBytes(byte[]? bytes);
C#
public static byte[] UrlEncodeToBytes(byte[] bytes);

Parameters

bytes
Byte[]

The array of bytes to encode.

Returns

Byte[]

An encoded array of bytes.

Remarks

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 embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

UrlEncodeToBytes(String)

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

Converts a string into a URL-encoded array of bytes.

C#
public static byte[]? UrlEncodeToBytes(string? str);
C#
public static byte[] UrlEncodeToBytes(string str);

Parameters

str
String

The string to encode.

Returns

Byte[]

An encoded array of bytes.

Remarks

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 embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

UrlEncodeToBytes(String, Encoding)

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

Converts a string into a URL-encoded array of bytes using the specified encoding object.

C#
public static byte[]? UrlEncodeToBytes(string? str, System.Text.Encoding e);
C#
public static byte[] UrlEncodeToBytes(string str, System.Text.Encoding e);

Parameters

str
String

The string to encode.

e
Encoding

The Encoding that specifies the encoding scheme.

Returns

Byte[]

An encoded array of bytes.

Remarks

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 embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

UrlEncodeToBytes(Byte[], Int32, Int32)

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

Converts an array of bytes into a URL-encoded array of bytes, starting at the specified position in the array and continuing for the specified number of bytes.

C#
public static byte[]? UrlEncodeToBytes(byte[]? bytes, int offset, int count);
C#
public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count);

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

Byte[]

An encoded array of bytes.

Exceptions

bytes is null, but count does not equal 0.

offset is less than 0 or greater than the length of the bytes array.

-or-

count is less than 0, or count + offset is greater than the length of the bytes array.

Remarks

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 embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1