HttpUtility 類別

定義

提供處理 Web 要求時用於編碼和解碼 URL 的方法。 此類別無法獲得繼承。

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

範例

下列程式碼範例示範 如何使用 UrlEncode 類別的 HttpUtilityUrlDecodeParseQueryString 方法。

<%@ 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 存取的編碼和解碼公用程式方法。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

建構函式

HttpUtility()

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

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HtmlAttributeEncode(String)

最低限度將字串轉換成 HTML 編碼字串。

HtmlAttributeEncode(String, TextWriter)

以最低限度將字串轉換成 HTML 編碼字串,並將編碼的字串傳送至 TextWriter 輸出資料流。

HtmlDecode(String)

將 HTTP 傳輸的 HTTP 編碼字串轉換成已解碼的字串。

HtmlDecode(String, TextWriter)

將已 HTML 編碼的字串轉換成已解碼的字串,並將解碼的字串傳送至 TextWriter 輸出資料流。

HtmlEncode(Object)

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

HtmlEncode(String)

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

HtmlEncode(String, TextWriter)

將字串轉換成 HTML 編碼字串,並以 TextWriter 輸出資料流的形式傳回輸出。

JavaScriptStringEncode(String)

編碼字串。

JavaScriptStringEncode(String, Boolean)

編碼字串。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ParseQueryString(String)

使用 UTF8 編碼方式將查詢字串剖析成 NameValueCollection

ParseQueryString(String, Encoding)

使用指定的 Encoding 將查詢字串剖析成 NameValueCollection

ToString()

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

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

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

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

使用指定的編碼物件,將 URL 編碼的位元組陣列轉換成已解碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。

UrlDecode(String)

將在 URL 中傳輸的已編碼字串轉換成已解碼的字串。

UrlDecode(String, Encoding)

使用指定的編碼物件將 URL 編碼的字串轉換成已解碼的字串。

UrlDecodeToBytes(Byte[])

將 URL 編碼的位元組陣列轉換成已解碼的位元組陣列。

UrlDecodeToBytes(Byte[], Int32, Int32)

將 URL 編碼的位元組陣列轉換成已解碼的位元組陣列,起點在陣列中的指定位置,延續至指定的位元組數目。

UrlDecodeToBytes(String)

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

UrlDecodeToBytes(String, Encoding)

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

UrlEncode(Byte[])

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

UrlEncode(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的字串,起點在陣列中的指定位置,延續至指定的位元組數目。

UrlEncode(String)

將 URL 字串編碼。

UrlEncode(String, Encoding)

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

UrlEncodeToBytes(Byte[])

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

UrlEncodeToBytes(Byte[], Int32, Int32)

將位元組陣列轉換成 URL 編碼的位元組陣列,起點在陣列中的指定位置,延續至指定的位元組數目。

UrlEncodeToBytes(String)

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

UrlEncodeToBytes(String, Encoding)

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

UrlEncodeUnicode(String)
已淘汰.

將字串轉換成 Unicode 字串。

UrlEncodeUnicodeToBytes(String)
已淘汰.

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

UrlPathEncode(String)

請勿使用,只在需要瀏覽器相容性時使用。 請使用 UrlEncode(String)

適用於

另請參閱