Export (0) Print
Expand All

HttpListenerContext Class

Provides access to the request and response objects used by the HttpListener class. This class cannot be inherited.

Namespace: System.Net
Assembly: System (in system.dll)

public sealed class HttpListenerContext
public final class HttpListenerContext
public final class HttpListenerContext
Not applicable.

This class provides the information related to a client's Hypertext Transfer Protocol (HTTP) request being processed by an HttpListener object. 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.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Service pack 2 is required to use the HttpListener class.

The following code example displays the user information for a client request.

public static bool RemoveAllPrefixes(HttpListener listener)
{
    // Get the prefixes that the Web server is listening to.
    HttpListenerPrefixCollection prefixes = listener.Prefixes;
    try 
    {
        prefixes.Clear();
    } 
    // If the operation failed, return false.
    catch
    {
        return false;
    }
    return true;
}

public static boolean RemoveAllPrefixes(HttpListener listener)
{
    // Get the prefixes that the Web server is listening to.
    HttpListenerPrefixCollection prefixes = listener.get_Prefixes();
    try {
        prefixes.Clear();
    }
    // If the operation failed, return false.
    catch (System.Exception exp) {
        return false;
    }
    return true;
} //RemoveAllPrefixes

System.Object
  System.Net.HttpListenerContext

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft