HttpListenerContext 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供对 HttpListener 类使用的请求和响应对象的访问。 此类不能被继承。
public ref class HttpListenerContext sealed
public sealed class HttpListenerContext
type HttpListenerContext = class
Public NotInheritable Class HttpListenerContext
- 继承
-
HttpListenerContext
示例
下面的代码示例显示客户端请求的用户信息。
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
注解
此类提供有关客户端的超文本传输协议的信息 (HTTP) 请求正在由 HttpListener 对象处理。 此类还具有允许对象 HttpListener 接受 WebSocket 连接的方法。
该方法 GetContext 返回类的 HttpListenerContext 实例。 若要获取表示客户端请求的对象,请使用 Request 该属性。 若要获取表示将由客户端 HttpListener发送的响应的对象,请使用该 Response 属性。 若要获取有关发送请求的客户端的用户信息,例如其登录名及其是否已进行身份验证,可以查询属性返回User的对象中的IPrincipal属性。
关闭对象 HttpListenerContext 会将响应发送到客户端,并释放由客户端 HttpListenerContext使用的任何资源。 如果尚未发送对象,则 HttpListenerContext 中止对象会放弃对客户端的响应,并释放由客户端 HttpListenerContext使用的任何资源。 关闭或中止对象 HttpListenerContext 后,不能引用其方法或属性。 如果这样做,将收到异常 ObjectDisposedException 。
属性
Request |
获取表示客户端对资源的请求的 HttpListenerRequest。 |
Response |
获取 HttpListenerResponse 对象,该对象将被发送到客户端以响应客户端的请求。 |
User |
获取用于为客户端获取标识、身份验证信息和安全角色的对象,该客户端的请求由此 HttpListenerContext 对象表示。 |
方法
AcceptWebSocketAsync(String) |
接受 WebSocket 连接以作为异步操作。 |
AcceptWebSocketAsync(String, Int32, TimeSpan) |
接受指定支持的 WebSocket 子协议的 WebSocket 连接、接受缓冲区大小和 WebSocket 保持活动状态的间隔以作为异步操作。 |
AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>) |
接受指定支持的 WebSocket 子协议的 WebSocket 连接、接受缓冲区大小和 WebSocket 保持活动状态的间隔和内部缓冲区以作为异步操作。 |
AcceptWebSocketAsync(String, TimeSpan) |
接受指定支持的 WebSocket 子协议和 WebSocket 保持活动状态的间隔的 WebSocket 连接以作为异步操作。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |