HttpListenerContext HttpListenerContext HttpListenerContext HttpListenerContext Class

Definition

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
Inheritance
HttpListenerContextHttpListenerContextHttpListenerContextHttpListenerContext

Examples

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;
}

Remarks

This class provides the information related to a client's Hypertext Transfer Protocol (HTTP) request being processed by an HttpListener object. This class also has methods that allow an HttpListener object to accept a WebSocket connection.

The GetContext method returns instances of the HttpListenerContext class. To get the object that represents the client request, use the Request property. To get the object that represents the response that will be sent to the client by the HttpListener, use the Response property. 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.

Closing an HttpListenerContext object sends the response to the client and frees any resources used by the 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. After closing or aborting an HttpListenerContext object, you cannot reference its methods or properties. If you do so, you will receive an ObjectDisposedException exception.

Properties

Request Request Request Request

Gets the HttpListenerRequest that represents a client's request for a resource.

Response Response Response Response

Gets the HttpListenerResponse object that will be sent to the client in response to the client's request.

User User User User

Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object.

Methods

AcceptWebSocketAsync(String) AcceptWebSocketAsync(String) AcceptWebSocketAsync(String) AcceptWebSocketAsync(String)

Accept a WebSocket connection as an asynchronous operation.

AcceptWebSocketAsync(String, Int32, TimeSpan) AcceptWebSocketAsync(String, Int32, TimeSpan) AcceptWebSocketAsync(String, Int32, TimeSpan) AcceptWebSocketAsync(String, Int32, TimeSpan)

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>) AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>) AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>) AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>)

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) AcceptWebSocketAsync(String, TimeSpan) AcceptWebSocketAsync(String, TimeSpan) AcceptWebSocketAsync(String, TimeSpan)

Accept a WebSocket connection specifying the supported WebSocket sub-protocol and WebSocket keep-alive interval as an asynchronous operation.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See Also