HttpServerUtility 类

定义

提供用于处理 Web 请求的 Helper 方法。Provides helper methods for processing Web requests.

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

示例

本主题提供了包含源代码的 Visual Studio 网站项目:下载A Visual Studio Web site project with source code is available to accompany this topic: Download.

下面的示例演示如何使用HtmlEncode HttpServerUtility类的方法UrlEncode和方法。The following example demonstrates how to use the HtmlEncode method and the UrlEncode method of the HttpServerUtility class. HtmlEncode方法有助于确保在浏览器中, 任何用户提供的字符串输入都将呈现为静态文本, 而不是可执行脚本或 HTML 元素。The HtmlEncode method helps ensure that any user-supplied string input will be rendered as static text in browsers instead of executable script or HTML elements. 方法UrlEncode对 url 进行编码, 以便在 HTTP 流中正确传输它们。The UrlEncode method encodes URLs so that they are correctly transmitted in the HTTP stream.

<%@ 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 Button1_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(TextBox1.Text))
        {
            // Access the HttpServerUtility methods through
            // the intrinsic Server object.
            Label1.Text = "Welcome, " +
                Server.HtmlEncode(TextBox1.Text) +
                ".<br/> The url is " + 
                Server.UrlEncode(Request.Url.ToString());
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpServerUtility Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter your name:<br />

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
        <br />
        <asp:Label ID="Label1" 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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        If (Not String.IsNullOrEmpty(TextBox1.Text)) Then
            
            ' Access the HttpServerUtility methods through
            ' the intrinsic Server object.
            Label1.Text = "Welcome, " & _
                Server.HtmlEncode(TextBox1.Text) & _
                ".<br/> The url is " & _
                Server.UrlEncode(Request.Url.ToString())
        End If
        
        
        
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HttpServerUtility Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter your name:<br />

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
        <br />
        <asp:Label ID="Label1" runat="server"/>
        </div>
    </form>
</body>
</html>

注解

HttpServerUtility类的方法和属性通过 ASP.NET 提供的内部Server对象公开。The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET.

属性

MachineName

获取服务器的计算机名称。Gets the server's computer name.

ScriptTimeout

获取或设置请求超时值(以秒为单位)。Gets or sets the request time-out value in seconds.

方法

ClearError()

清除前一个异常。Clears the previous exception.

CreateObject(String)

创建 COM 对象的服务器实例,该 COM 对象由对象的程序标识符 (ProgID) 标识。Creates a server instance of a COM object identified by the object's programmatic identifier (ProgID).

CreateObject(Type)

创建由对象类型标识的 COM 对象的一个服务器实例。Creates a server instance of a COM object identified by the object's type.

CreateObjectFromClsid(String)

创建 COM 对象的服务器实例,该对象由对象的类标识符 (CLSID) 标识。Creates a server instance of a COM object identified by the object's class identifier (CLSID).

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Execute(IHttpHandler, TextWriter, Boolean)

在当前请求的上下文中执行指定虚拟路径的处理程序。Executes the handler for the specified virtual path in the context of the current request. TextWriter 捕获执行的处理程序的输出,布尔参数则指定是否清除 QueryStringForm 集合。A TextWriter captures output from the executed handler and a Boolean parameter specifies whether to clear the QueryString and Form collections.

Execute(String)

在当前请求的上下文中执行指定虚拟路径的处理程序。Executes the handler for the specified virtual path in the context of the current request.

Execute(String, Boolean)

在当前请求的上下文中执行指定虚拟路径的处理器,并指定是否清除 QueryStringForm 集合。Executes the handler for the specified virtual path in the context of the current request and specifies whether to clear the QueryString and Form collections.

Execute(String, TextWriter)

在当前请求的上下文中执行指定虚拟路径的处理程序。Executes the handler for the specified virtual path in the context of the current request. TextWriter 捕获执行的处理程序的输出。A TextWriter captures output from the executed handler.

Execute(String, TextWriter, Boolean)

在当前请求的上下文中执行指定虚拟路径的处理程序。Executes the handler for the specified virtual path in the context of the current request. TextWriter 捕获页面输出,布尔参数则指定是否清除 QueryStringForm 集合。A TextWriter captures output from the page and a Boolean parameter specifies whether to clear the QueryString and Form collections.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLastError()

返回前一个异常。Returns the previous exception.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
HtmlDecode(String)

对 HTML 编码的字符串进行解码,并返回已解码的字符串。Decodes an HTML-encoded string and returns the decoded string.

HtmlDecode(String, TextWriter)

对 HTML 编码的字符串进行解码,并将结果输出发送到 TextWriter 输出流。Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.

HtmlEncode(String)

对字符串进行 HTML 编码并返回已编码的字符串。HTML-encodes a string and returns the encoded string.

HtmlEncode(String, TextWriter)

对字符串进行 HTML 解码,并将结果输出发送到 TextWriter 输出流。HTML-encodes a string and sends the resulting output to a TextWriter output stream.

MapPath(String)

返回与指定虚拟路径相对应的物理文件路径。Returns the physical file path that corresponds to the specified virtual path.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
Transfer(IHttpHandler, Boolean)

终止当前页的执行,然后使用实现 IHttpHandler 接口的自定义 HTTP 处理程序开始新请求的执行,并指定是否清除 QueryStringForm 集合。Terminates execution of the current page and starts execution of a new request by using a custom HTTP handler that implements the IHttpHandler interface and specifies whether to clear the QueryString and Form collections.

Transfer(String)

对于当前请求,终止当前页的执行,并使用指定的页 URL 路径来开始执行一个新页。For the current request, terminates execution of the current page and starts execution of a new page by using the specified URL path of the page.

Transfer(String, Boolean)

终止当前页的执行,并使用指定的页 URL 路径来开始执行一个新页。Terminates execution of the current page and starts execution of a new page by using the specified URL path of the page. 指定是否清除 QueryStringForm 集合。Specifies whether to clear the QueryString and Form collections.

TransferRequest(String)

异步执行指定的 URL。Performs an asynchronous execution of the specified URL.

TransferRequest(String, Boolean)

异步执行指定的 URL 并保留查询字符串参数。Performs an asynchronous execution of the specified URL and preserves query string parameters.

TransferRequest(String, Boolean, String, NameValueCollection)

使用指定的 HTTP 方法和标头异步执行指定的 URL。Performs an asynchronous execution of the specified URL using the specified HTTP method and headers.

TransferRequest(String, Boolean, String, NameValueCollection, Boolean)

用指定的 HTTP 方法、标头和路径,执行指定的 URL 上的异步执行和选择保留格式值和用户标识。Performs an asynchronous execution of the specified URL using the specified HTTP method, headers, and path, and optionally preserves form values and the user identity.

UrlDecode(String)

对字符串进行 URL 解码并返回已解码的字符串。URL-decodes a string and returns the decoded string.

UrlDecode(String, TextWriter)

对在 URL 中接收的 HTML 字符串进行解码,并将结果输出发送到 TextWriter 输出流。Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.

UrlEncode(String)

对字符串进行 URL 编码,并返回已编码的字符串。URL-encodes a string and returns the encoded string.

UrlEncode(String, TextWriter)

对字符串进行 URL 编码,并将结果输出发送到 TextWriter 输出流。URL-encodes a string and sends the resulting output to a TextWriter output stream.

UrlPathEncode(String)

请勿使用;仅适用于浏览器兼容性。Do not use; intended only for browser compatibility. 请使用 UrlEncode(String)Use UrlEncode(String).

UrlTokenDecode(String)

将 URL 字符串标记解码为使用 64 进制数字的等效字节数组。Decodes a URL string token to its equivalent byte array using base 64 digits.

UrlTokenEncode(Byte[])

将一个字节数组编码为使用 Base 64 编码方案的等效字符串表示形式,Base 64 是一种适于通过 URL 传输数据的编码方案。Encodes a byte array into its equivalent string representation using base 64 digits, which is usable for transmission on the URL.

适用于

另请参阅