HttpUtility 類別

定義

提供處理網頁請求時編碼與解碼 URL 的方法。 此類別無法獲得繼承。

public ref class HttpUtility sealed
public sealed class HttpUtility
type HttpUtility = class
Public NotInheritable Class HttpUtility
繼承
HttpUtility

範例

以下程式碼範例展示了該類別 的使用 UrlEncodeUrlDecode 以及 ParseQueryStringHttpUtility 方法。

<%@ 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 物件揭露。 此外,該 HttpUtility 類別還包含無法從 Server中存取的編碼與解碼效用方法。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

建構函式

名稱 Description
HttpUtility()

初始化 HttpUtility 類別的新執行個體。

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
HtmlAttributeEncode(String, TextWriter)

至少將字串轉換成 HTML 編碼的字串,並將編碼後的字串傳送到 TextWriter 輸出串流。

HtmlAttributeEncode(String)

至少將字串轉換成 HTML 編碼的字串。

HtmlDecode(String, TextWriter)

將已編碼成 HTML 編碼的字串轉換為解碼後的字串,並將解碼後的字串傳送到 TextWriter 輸出串流。

HtmlDecode(String)

將已以 HTML 編碼用於 HTTP 傳輸的字串轉換為解碼字串。

HtmlEncode(Object)

將物件的字串表示轉換為 HTML 編碼的字串,並回傳編碼後的字串。

HtmlEncode(String, TextWriter)

將字串轉換成 HTML 編碼的字串,並以輸出串流回傳 TextWriter

HtmlEncode(String)

將字串轉換成 HTML 編碼字串。

JavaScriptStringEncode(String, Boolean)

編碼一個字串。

JavaScriptStringEncode(String)

編碼一個字串。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ParseQueryString(String, Encoding)

利用指定的 Encoding解析查詢字串 NameValueCollection

ParseQueryString(String)

利用編碼解析查詢字串。NameValueCollectionUTF8

ToString()

傳回表示目前 物件的字串。

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

利用指定的解碼物件,將 URL 編碼的位元組陣列轉換成解碼後的字串。

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

將 URL 編碼的位元組陣列轉換為解碼字串,使用指定的編碼物件,從陣列中指定位置開始,並持續執行指定位元組數。

UrlDecode(String, Encoding)

利用指定的編碼物件,將 URL 編碼的字串轉換為解碼字串。

UrlDecode(String)

將已編碼用於 URL 傳輸的字串轉換為解碼字串。

UrlDecodeToBytes(Byte[], Int32, Int32)

將一個 URL 編碼的位元組陣列轉換成一個解碼後的位元組陣列,從陣列中指定的位置開始,並持續到指定位元組數。

UrlDecodeToBytes(Byte[])

將一個 URL 編碼的位元組陣列轉換成解碼後的位元組陣列。

UrlDecodeToBytes(String, Encoding)

利用指定的解碼物件將 URL 編碼的字串轉換成解碼後的位元組陣列。

UrlDecodeToBytes(String)

將 URL 編碼的字串轉換為解碼後的位元組陣列。

UrlEncode(Byte[], Int32, Int32)

將一個位元組陣列轉換成 URL 編碼的字串,從陣列中指定位置開始,並持續到指定位元組數。

UrlEncode(Byte[])

將位元組陣列轉換成編碼的 URL 字串。

UrlEncode(String, Encoding)

使用指定的編碼物件來編碼 URL 字串。

UrlEncode(String)

編碼一個網址字串。

UrlEncodeToBytes(Byte[], Int32, Int32)

將一個位元組陣列轉換成一個 URL 編碼的位元組陣列,從陣列中指定位置開始,並持續到指定位元組數。

UrlEncodeToBytes(Byte[])

將一個位元組陣列轉換成一個 URL 編碼的位元組陣列。

UrlEncodeToBytes(String, Encoding)

利用指定的編碼物件將字串轉換成 URL 編碼的位元組陣列。

UrlEncodeToBytes(String)

將字串轉換成 URL 編碼的位元組陣列。

UrlEncodeUnicode(String)
已淘汰.

將字串轉換成 Unicode 字串。

UrlEncodeUnicodeToBytes(String)
已淘汰.

將 Unicode 字串轉換成一個位元組陣列。

UrlPathEncode(String)

請勿使用;僅為瀏覽器相容性設計。 請使用 UrlEncode(String)

適用於

另請參閱