HttpUtility Class

Definition

Provides methods for encoding and decoding URLs when processing Web requests. This class cannot be inherited.

public sealed class HttpUtility
Inheritance
HttpUtility

Examples

The following code example demonstrates the use of the UrlEncode, UrlDecode and ParseQueryString methods of the HttpUtility class.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        String currurl = HttpContext.Current.Request.RawUrl;
        String querystring = null;

        // Check to make sure some query string variables
        // exist and if not add some and redirect.
        int iqs = currurl.IndexOf('?');
        if (iqs == -1)
        {
            String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
            Response.Redirect(redirecturl, true);
        }
        // If query string variables exist, put them in
        // a string.
        else if (iqs >= 0)
        {
            querystring = (iqs < currurl.Length - 1) ? currurl.Substring(iqs + 1) : String.Empty;
        }

        // Parse the query string variables into a NameValueCollection.
        NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

        // Iterate through the collection.
        StringBuilder sb = new StringBuilder();
        foreach (String s in qscoll.AllKeys)
        {
            sb.Append(s + " - " + qscoll[s] + "<br />");
        }

        // Write the results to the appropriate labels.
        ParseOutput.Text = sb.ToString();
        UrlRawOutput.Text = currurl;
        UrlEncodedOutput.Text = HttpUtility.UrlEncode(currurl);
        UrlDecodedOutput.Text = HttpUtility.UrlDecode(currurl);
    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpUtility Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      The raw url is: <br />
      <asp:Label  id="UrlRawOutput"
                  runat="server" />
      <br /><br />
      The url encoded is: <br />
      <asp:Label  id="UrlEncodedOutput"
                  runat="server" />
      <br /><br />
      The url decoded is: <br />
      <asp:Label  id="UrlDecodedOutput"
                  runat="server" />
      <br /><br />
      The query string NameValueCollection is: <br />
      <asp:Label  id="ParseOutput"
                  runat="server" />
    </div>
    </form>
</body>
</html>

Remarks

The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.

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

Constructors

HttpUtility()

Initializes a new instance of the HttpUtility class.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HtmlAttributeEncode(String, TextWriter)

Minimally converts a string into an HTML-encoded string and sends the encoded string to a TextWriter output stream.

HtmlAttributeEncode(String)

Minimally converts a string to an HTML-encoded string.

HtmlDecode(String, TextWriter)

Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.

HtmlDecode(String)

Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

HtmlEncode(Object)

Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

HtmlEncode(String, TextWriter)

Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

HtmlEncode(String)

Converts a string to an HTML-encoded string.

JavaScriptStringEncode(String, Boolean)

Encodes a string.

JavaScriptStringEncode(String)

Encodes a string.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ParseQueryString(String, Encoding)

Parses a query string into a NameValueCollection using the specified Encoding.

ParseQueryString(String)

Parses a query string into a NameValueCollection using UTF8 encoding.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
UrlDecode(Byte[], Encoding)

Converts a URL-encoded byte array into a decoded string using the specified decoding object.

UrlDecode(Byte[], Int32, Int32, Encoding)

Converts a URL-encoded byte array into a decoded string using the specified encoding object, starting at the specified position in the array, and continuing for the specified number of bytes.

UrlDecode(String, Encoding)

Converts a URL-encoded string into a decoded string, using the specified encoding object.

UrlDecode(String)

Converts a string that has been encoded for transmission in a URL into a decoded string.

UrlDecodeToBytes(Byte[], Int32, Int32)

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

UrlDecodeToBytes(Byte[])

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

UrlDecodeToBytes(String, Encoding)

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

UrlDecodeToBytes(String)

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

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

Converts a byte array into an encoded URL string.

UrlEncode(String, Encoding)

Encodes a URL string using the specified encoding object.

UrlEncode(String)

Encodes a URL string.

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

Converts an array of bytes 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(String)

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

UrlEncodeUnicode(String)
Obsolete.

Converts a string into a Unicode string.

UrlEncodeUnicodeToBytes(String)
Obsolete.

Converts a Unicode string into an array of bytes.

UrlPathEncode(String)

Do not use; intended only for browser compatibility. Use UrlEncode(String).

Applies to

Izdelek Različice
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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

See also