HttpUtility 類別

定義

提供處理 Web 要求時用於編碼和解碼 URL 的方法。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
繼承
HttpUtility

範例

下列程式碼範例將示範如何使用 UrlEncode 類別的 UrlDecodeParseQueryString 方法 HttpUtilityThe 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>

備註

HttpUtility類別是由類別在內部使用 HttpServerUtility ,其方法和屬性會透過內建函式(ASP.NET) Server 物件公開。The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. 此外, HttpUtility 類別還包含無法從存取的編碼和解碼公用程式方法 ServerAdditionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

建構函式

HttpUtility()

初始化 HttpUtility 類別的新執行個體。Initializes a new instance of the HttpUtility class.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
HtmlAttributeEncode(String)

最低限度將字串轉換成 HTML 編碼字串。Minimally converts a string to an HTML-encoded string.

HtmlAttributeEncode(String, TextWriter)

以最低限度將字串轉換成 HTML 編碼字串,並將編碼的字串傳送至 TextWriter 輸出資料流。Minimally converts a string into an HTML-encoded string and sends the encoded string to a TextWriter output stream.

HtmlDecode(String)

將 HTTP 傳輸的 HTTP 編碼字串轉換成已解碼的字串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

HtmlDecode(String, TextWriter)

將已 HTML 編碼的字串轉換成已解碼的字串,並將解碼的字串傳送至 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)

將物件的字串表示轉換成 HTML 編碼字串,並傳回此編碼的字串。Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

HtmlEncode(String)

將字串轉換成 HTML 編碼字串。Converts a string to an HTML-encoded string.

HtmlEncode(String, TextWriter)

將字串轉換成 HTML 編碼字串,並以 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()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ParseQueryString(String)

使用 UTF8 編碼方式將查詢字串剖析成 NameValueCollectionParses a query string into a NameValueCollection using UTF8 encoding.

ParseQueryString(String, Encoding)

使用指定的 Encoding 將查詢字串剖析成 NameValueCollectionParses a query string into a NameValueCollection using the specified Encoding.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
UrlDecode(Byte[], Encoding)

使用指定的解碼物件將 URL 編碼的位元組陣列轉換成已解碼的字串。Converts a URL-encoded byte array into a decoded string using the specified decoding object.

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

使用指定的編碼物件,將 URL 編碼的位元組陣列轉換成已解碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。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)

將在 URL 中傳輸的已編碼字串轉換成已解碼的字串。Converts a string that has been encoded for transmission in a URL into a decoded string.

UrlDecode(String, Encoding)

使用指定的編碼物件將 URL 編碼的字串轉換成已解碼的字串。Converts a URL-encoded string into a decoded string, using the specified encoding object.

UrlDecodeToBytes(Byte[])

將 URL 編碼的位元組陣列轉換成已解碼的位元組陣列。Converts a URL-encoded array of bytes into a decoded array of bytes.

UrlDecodeToBytes(Byte[], Int32, Int32)

將 URL 編碼的位元組陣列轉換成已解碼的位元組陣列,起點在陣列中的指定位置,延續至指定的位元組數目。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)

將 URL 編碼的字串轉換成解碼的位元組陣列。Converts a URL-encoded string into a decoded array of bytes.

UrlDecodeToBytes(String, Encoding)

使用指定的解碼物件將 URL 編碼的字串轉換成已解碼的位元組陣列。Converts a URL-encoded string into a decoded array of bytes using the specified decoding object.

UrlEncode(Byte[])

將位元組陣列轉換成編碼的 URL 字串。Converts a byte array into an encoded URL string.

UrlEncode(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。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)

將 URL 字串編碼。Encodes a URL string.

UrlEncode(String, Encoding)

使用指定的編碼物件,編碼 URL 字串。Encodes a URL string using the specified encoding object.

UrlEncodeToBytes(Byte[])

將位元組陣列轉換成 URL 編碼的位元組陣列。Converts an array of bytes into a URL-encoded array of bytes.

UrlEncodeToBytes(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的位元組陣列,起點在陣列中的指定位置,延續至指定的位元組數目。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)

將字串轉換為 URL 編碼的位元組陣列。Converts a string into a URL-encoded array of bytes.

UrlEncodeToBytes(String, Encoding)

使用指定的編碼物件將字串轉換成 URL 編碼的位元組陣列。Converts a string into a URL-encoded array of bytes using the specified encoding object.

UrlEncodeUnicode(String)
已過時。

將字串轉換成 Unicode 字串。Converts a string into a Unicode string.

UrlEncodeUnicodeToBytes(String)
已過時。

將 Unicode 字串轉換成位元組陣列。Converts a Unicode string into an array of bytes.

UrlPathEncode(String)

請勿使用,只在需要瀏覽器相容性時使用。Do not use; intended only for browser compatibility. 請使用 UrlEncode(String)Use UrlEncode(String).

適用於

另請參閱