Share via


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.

Inheritance Hierarchy

System. . :: . .Object
  System.Net..::..HttpListener

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

Syntax

'Declaration
Public Class HttpListener
public class HttpListener
public ref class HttpListener
type HttpListener =  class end
public class HttpListener

The HttpListener type exposes the following members.

Constructors

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

Top

Properties

  Name Description
Public property HttpsCert Gets or sets the certificate that is used, if HttpListener implements an HTTPS server.
Public property IsListening Gets a value that indicates whether HttpListener has been started.
Public property MaximumResponseHeadersLength Gets or sets the maximum allowed length of the response headers, in KB.

Top

Methods

  Name Description
Public method Abort Shuts down the HttpListener object immediately, discarding all currently queued requests.
Public method Close Shuts down the HttpListener.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetContext Waits for an incoming request and returns when one is received.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Start Allows this instance to receive incoming requests.
Public method Stop Causes this instance to stop receiving incoming requests.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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 "https://www.contoso.com:8080/customerData/". 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.

Thread Safety

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

See Also

Reference

System.Net Namespace