HttpUtility 类

定义

提供在处理 Web 请求时用于编码和解码 URL 的方法。 此类不能被继承。

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

示例

下面的代码示例演示了类的HttpUtility用法UrlEncodeUrlDecodeParseQueryString方法。

<%@ 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 传输进行过 HTML 编码的字符串转换为已解码的字符串。

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)

适用于

另请参阅