HttpListenerContext 类

定义

提供对 HttpListener 类使用的请求和响应对象的访问。Provides access to the request and response objects used by the HttpListener class. 无法继承此类。This class cannot be inherited.

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

示例

下面的代码示例显示客户端请求的用户信息。The following code example displays the user information for a client request.

public static string ClientInformation(HttpListenerContext context)
{
    System.Security.Principal.IPrincipal user = context.User;
    System.Security.Principal.IIdentity id = user.Identity;
    if (id == null)
    {
        return "Client authentication is not enabled for this Web server.";
    }
    
    string display;
    if (id.IsAuthenticated)
    {
        display = String.Format("{0} was authenticated using {1}", id.Name, 
            id.AuthenticationType);
    }
    else
    {
       display = String.Format("{0} was not authenticated", id.Name);
    }
    return display;
}
Public Shared Function ClientInformation(ByVal context As HttpListenerContext) As String
    Dim user As System.Security.Principal.IPrincipal = context.User
    Dim id As System.Security.Principal.IIdentity = user.Identity

    If id Is Nothing Then
        Return "Client authentication is not enabled for this Web server."
    End If

    Dim display As String

    If id.IsAuthenticated Then
        display = String.Format("{0} was authenticated using {1}", id.Name, id.AuthenticationType)
    Else
        display = String.Format("{0} was not authenticated", id.Name)
    End If

    Return display
End Function

注解

此类提供与 HttpListener 对象处理的客户端超文本传输协议(HTTP)请求相关的信息。This class provides the information related to a client's Hypertext Transfer Protocol (HTTP) request being processed by an HttpListener object. 此类还包含允许 HttpListener 对象接受 WebSocket 连接的方法。This class also has methods that allow an HttpListener object to accept a WebSocket connection.

GetContext 方法返回 HttpListenerContext 类的实例。The GetContext method returns instances of the HttpListenerContext class. 若要获取表示客户端请求的对象,请使用 Request 属性。To get the object that represents the client request, use the Request property. 若要获取表示将由 HttpListener发送到客户端的响应的对象,请使用 Response 属性。To get the object that represents the response that will be sent to the client by the HttpListener, use the Response property. 若要获取有关发送请求的客户端的用户信息(如其登录名和是否经过身份验证),可以在 User 属性返回的 IPrincipal 对象中查询属性。To get user information about the client sending the request, such as its login name and whether it has been authenticated, you can query the properties in the IPrincipal object returned by the User property.

关闭 HttpListenerContext 对象会将响应发送到客户端,并释放 HttpListenerContext使用的所有资源。Closing an HttpListenerContext object sends the response to the client and frees any resources used by the HttpListenerContext. 中止 HttpListenerContext 对象会放弃对客户端的响应(如果尚未发送),并释放 HttpListenerContext使用的所有资源。Aborting an HttpListenerContext object discards the response to the client if it has not already been sent, and frees any resources used by the HttpListenerContext. 关闭或中止 HttpListenerContext 对象之后,将无法引用其方法或属性。After closing or aborting an HttpListenerContext object, you cannot reference its methods or properties. 如果这样做,则会收到 ObjectDisposedException 异常。If you do so, you will receive an ObjectDisposedException exception.

属性

Request

获取表示客户端对资源的请求的 HttpListenerRequestGets the HttpListenerRequest that represents a client's request for a resource.

Response

获取 HttpListenerResponse 对象,该对象将被发送到客户端以响应客户端的请求。Gets the HttpListenerResponse object that will be sent to the client in response to the client's request.

User

获取用于为客户端获取标识、身份验证信息和安全角色的对象,该客户端的请求由此 HttpListenerContext 对象表示。Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object.

方法

AcceptWebSocketAsync(String)

接受 WebSocket 连接以作为异步操作。Accept a WebSocket connection as an asynchronous operation.

AcceptWebSocketAsync(String, Int32, TimeSpan)

接受指定支持的 WebSocket 子协议的 WebSocket 连接、接受缓冲区大小和 WebSocket 保持活动状态的间隔以作为异步操作。Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, and WebSocket keep-alive interval as an asynchronous operation.

AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>)

接受指定支持的 WebSocket 子协议的 WebSocket 连接、接受缓冲区大小和 WebSocket 保持活动状态的间隔和内部缓冲区以作为异步操作。Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, WebSocket keep-alive interval, and the internal buffer as an asynchronous operation.

AcceptWebSocketAsync(String, TimeSpan)

接受指定支持的 WebSocket 子协议和 WebSocket 保持活动状态的间隔的 WebSocket 连接以作为异步操作。Accept a WebSocket connection specifying the supported WebSocket sub-protocol and WebSocket keep-alive interval as an asynchronous operation.

Equals(Object)

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

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

另请参阅