This topic has not yet been rated - Rate this topic

HttpUtility Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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

System.Object
  System.Web.HttpUtility

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

public sealed class HttpUtility

The HttpUtility type exposes the following members.

  Name Description
Public method HttpUtility Initializes a new instance of the HttpUtility class.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Static member HtmlAttributeEncode(String) Minimally converts a string to an HTML-encoded string.
Public method Static member HtmlAttributeEncode(String, TextWriter) Minimally converts a string into an HTML-encoded string and sends the encoded string to a TextWriter output stream.
Public method Static member HtmlDecode(String) Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
Public method Static member 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.
Public method Static member HtmlEncode(Object) Converts an object's string representation into an HTML-encoded string, and returns the encoded string.
Public method Static member HtmlEncode(String) Converts a string to an HTML-encoded string.
Public method Static member HtmlEncode(String, TextWriter) Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.
Public method Static member JavaScriptStringEncode(String) Encodes a string.
Public method Static member JavaScriptStringEncode(String, Boolean) Encodes a string.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member ParseQueryString(String) Parses a query string into a NameValueCollection using UTF8 encoding.
Public method Static member ParseQueryString(String, Encoding) Parses a query string into a NameValueCollection using the specified Encoding.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Static member UrlDecode(String) Converts a string that has been encoded for transmission in a URL into a decoded string.
Public method Static member UrlDecode(Byte[], Encoding) Converts a URL-encoded byte array into a decoded string using the specified decoding object.
Public method Static member UrlDecode(String, Encoding) Converts a URL-encoded string into a decoded string, using the specified encoding object.
Public method Static member 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.
Public method Static member UrlDecodeToBytes(Byte[]) Converts a URL-encoded array of bytes into a decoded array of bytes.
Public method Static member UrlDecodeToBytes(String) Converts a URL-encoded string into a decoded array of bytes.
Public method Static member UrlDecodeToBytes(String, Encoding) Converts a URL-encoded string into a decoded array of bytes using the specified decoding object.
Public method Static member 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.
Public method Static member UrlEncode(Byte[]) Converts a byte array into an encoded URL string.
Public method Static member UrlEncode(String) Encodes a URL string.
Public method Static member UrlEncode(String, Encoding) Encodes a URL string using the specified encoding object.
Public method Static member 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.
Public method Static member UrlEncodeToBytes(Byte[]) Converts an array of bytes into a URL-encoded array of bytes.
Public method Static member UrlEncodeToBytes(String) Converts a string into a URL-encoded array of bytes.
Public method Static member UrlEncodeToBytes(String, Encoding) Converts a string into a URL-encoded array of bytes using the specified encoding object.
Public method Static member 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.
Public method Static member UrlEncodeUnicode Obsolete. Converts a string into a Unicode string.
Public method Static member UrlEncodeUnicodeToBytes Obsolete. Converts a Unicode string into an array of bytes.
Public method Static member UrlPathEncode Encodes the path portion of a URL string for reliable HTTP transmission from the Web server to a client.
Top

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.

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

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8 Release Preview, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)