HttpListener Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Provides a simple, programmatically controlled HTTP protocol listener. This class cannot be inherited.


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

public class HttpListener

The HttpListener type exposes the following members.

Public methodHttpListener(String)Initializes a new instance of the HttpListener class with the standard ports.
Public methodHttpListener(String, Int32)Initializes a new instance of the HttpListener class with the specified port.

Public propertyHttpsCertGets or sets the certificate that is used, if HttpListener implements an HTTPS server.
Public propertyIsListeningGets a value that indicates whether HttpListener has been started.
Public propertyMaximumResponseHeadersLengthGets or sets the maximum allowed length of the response headers, in KB.

Public methodAbortShuts down the HttpListener object immediately, discarding all currently queued requests.
Public methodCloseShuts down the HttpListener.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetContextWaits for an incoming request and returns when one is received.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStartAllows this instance to receive incoming requests.
Public methodStopCauses this instance to stop receiving incoming requests.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Using the HttpListener class, you can create a simple HTTP protocol listener that responds to HTTP requests. The listener is active for the lifetime of the HttpListener object and runs within your application with its permissions.

To use HttpListener, create a new instance of the class using the HttpListener constructor and use the Prefixes property to gain access to the collection that holds the strings that specify which Uniform Resource Identifier (URI) prefixes the HttpListener should process.

A URI prefix string is composed of a scheme (http or https), a host, an optional port, and an optional path. An example of a complete prefix string is "". Prefixes must end in a forward slash ("/"). The HttpListener object with the prefix that most closely matches a requested URI responds to the request.

When a port is specified, the host element can be replaced with "*" to indicate that the HttpListener accepts requests sent to the port if the requested URI does not match any other prefix. For example, to receive all requests sent to port 8080 when the requested URI is not handled by any HttpListener, the prefix is "http://*:8080/". Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the "+" character, "https://+:8080". The "*" and "+" characters can be present in prefixes that include paths.

To begin listening for requests from clients, add the URI prefixes to the collection and call the Start method.

The synchronous model is appropriate if your application should block while waiting for a client request and if you want to process only one request at a time. Using the synchronous model, call the GetContext method, which waits for a client to send a request. The method returns an HttpListenerContext object to you for processing when one occurs.

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