HttpUtility Class

 

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

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

System.Object
  System.Web.HttpUtility

public sealed class HttpUtility

NameDescription
System_CAPS_pubmethodHttpUtility()

Initializes a new instance of the HttpUtility class.

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticHtmlAttributeEncode(String)

Minimally converts a string to an HTML-encoded string.

System_CAPS_pubmethodSystem_CAPS_staticHtmlAttributeEncode(String, TextWriter)

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

System_CAPS_pubmethodSystem_CAPS_staticHtmlDecode(String)

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

System_CAPS_pubmethodSystem_CAPS_staticHtmlDecode(String, TextWriter)

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

System_CAPS_pubmethodSystem_CAPS_staticHtmlEncode(Object)

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

System_CAPS_pubmethodSystem_CAPS_staticHtmlEncode(String)

Converts a string to an HTML-encoded string.

System_CAPS_pubmethodSystem_CAPS_staticHtmlEncode(String, TextWriter)

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

System_CAPS_pubmethodSystem_CAPS_staticJavaScriptStringEncode(String)

Encodes a string.

System_CAPS_pubmethodSystem_CAPS_staticJavaScriptStringEncode(String, Boolean)

Encodes a string.

System_CAPS_pubmethodSystem_CAPS_staticParseQueryString(String)

Parses a query string into a NameValueCollection using UTF8 encoding.

System_CAPS_pubmethodSystem_CAPS_staticParseQueryString(String, Encoding)

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

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticUrlDecode(Byte[], Encoding)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlDecode(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.

System_CAPS_pubmethodSystem_CAPS_staticUrlDecode(String)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlDecode(String, Encoding)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlDecodeToBytes(Byte[])

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

System_CAPS_pubmethodSystem_CAPS_staticUrlDecodeToBytes(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.

System_CAPS_pubmethodSystem_CAPS_staticUrlDecodeToBytes(String)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlDecodeToBytes(String, Encoding)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlEncode(Byte[])

Converts a byte array into an encoded URL string.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncode(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.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncode(String)

Encodes a URL string.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncode(String, Encoding)

Encodes a URL string using the specified encoding object.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeToBytes(Byte[])

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

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeToBytes(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.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeToBytes(String)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeToBytes(String, Encoding)

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

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeUnicode(String)

Obsolete.Converts a string into a Unicode string.

System_CAPS_pubmethodSystem_CAPS_staticUrlEncodeUnicodeToBytes(String)

Obsolete.Converts a Unicode string into an array of bytes.

System_CAPS_pubmethodSystem_CAPS_staticUrlPathEncode(String)

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

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.

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>

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: