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 | |
---|---|---|
HttpListener(String) | Initializes a new instance of the HttpListener class with the standard ports. | |
HttpListener(String, Int32) | Initializes a new instance of the HttpListener class with the specified port. |
Top
Properties
Name | Description | |
---|---|---|
HttpsCert | Gets or sets the certificate that is used, if HttpListener implements an HTTPS server. | |
IsListening | Gets a value that indicates whether HttpListener has been started. | |
MaximumResponseHeadersLength | Gets or sets the maximum allowed length of the response headers, in KB. |
Top
Methods
Name | Description | |
---|---|---|
Abort | Shuts down the HttpListener object immediately, discarding all currently queued requests. | |
Close | Shuts down the HttpListener. | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetContext | Waits for an incoming request and returns when one is received. | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Start | Allows this instance to receive incoming requests. | |
Stop | Causes this instance to stop receiving incoming requests. | |
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.