Upraviť

Zdieľať cez


HttpUtility Class

Definition

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

public ref class HttpUtility sealed
public sealed class HttpUtility
type HttpUtility = class
Public NotInheritable 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>
<%@ Page Language="VB" %>

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

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim currurl As String = HttpContext.Current.Request.RawUrl
        Dim querystring As String = Nothing
    
        ' Check to make sure some query string variables
        ' exist and if not add some and redirect.
        Dim iqs As Int32 = currurl.IndexOf("?".ToCharArray())
        If (iqs = -1) Then
      
            Dim redirecturl As String = currurl & "?var1=1&var2=2+2%2f3&var1=3"
            Response.Redirect(redirecturl, True)
      
            ' If query string variables exist, put them in
            ' a string.
        ElseIf (iqs >= 0) Then
      
            If (iqs < currurl.Length - 1) Then
                querystring = currurl.Substring(iqs + 1)
            End If
          
        End If

        ' Parse the query string variables into a NameValueCollection.
        Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
    
        ' Iterate through the collection.
        Dim sb As New StringBuilder()
        For Each s As String In qscoll.AllKeys
      
            sb.Append(s & " - " & qscoll(s) & "<br />")
    
        Next s
    
        ' Write the results to the appropriate labels.
        ParseOutput.Text = sb.ToString()
        UrlRawOutput.Text = currurl
        UrlEncodedOutput.Text = HttpUtility.UrlEncode(currurl)
        UrlDecodedOutput.Text = HttpUtility.UrlDecode(currurl)
   
    End Sub
</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)

Minimally converts a string to an HTML-encoded string.

HtmlAttributeEncode(String, TextWriter)

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

HtmlDecode(String)

Converts a string that has been HTML-encoded for HTTP transmission into a decoded 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.

HtmlEncode(Object)

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

HtmlEncode(String)

Converts a string to an HTML-encoded string.

HtmlEncode(String, TextWriter)

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

JavaScriptStringEncode(String)

Encodes a string.

JavaScriptStringEncode(String, Boolean)

Encodes a string.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ParseQueryString(String)

Parses a query string into a NameValueCollection using UTF8 encoding.

ParseQueryString(String, Encoding)

Parses a query string into a NameValueCollection using the specified 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)

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

UrlDecode(String, Encoding)

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

UrlDecodeToBytes(Byte[])

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

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

Converts a URL-encoded string 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.

UrlEncode(Byte[])

Converts a byte array into an encoded URL string.

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)

Encodes a URL string.

UrlEncode(String, Encoding)

Encodes a URL string using the specified encoding object.

UrlEncodeToBytes(Byte[])

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

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

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

See also