HttpUtility 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供在处理 Web 请求时用于编码和解码 URL 的方法。 此类不能被继承。
public ref class HttpUtility sealed
public sealed class HttpUtility
type HttpUtility = class
Public NotInheritable Class HttpUtility
- 继承
-
HttpUtility
示例
下面的代码示例演示如何使用 类的 UrlEncode、 UrlDecode 和 ParseQueryString 方法 HttpUtility 。
<%@ 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 类的新实例。 |