HttpUtility 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供處理 Web 要求時用於編碼和解碼 URL 的方法。 此類別無法獲得繼承。
public ref class HttpUtility sealed
public sealed class HttpUtility
type HttpUtility = class
Public NotInheritable Class HttpUtility
- 繼承
-
HttpUtility
範例
下列程式代碼範例示範 如何使用 UrlEncode類別的 HttpUtility 、 UrlDecode 和 ParseQueryString 方法。
<%@ 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 類別的新執行個體。 |