Table of Contents

Class HttpListener

Namespace
System.Net
Assembly
System.Net.Http.dll

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

public class HttpListener
Inheritance
HttpListener
Inherited Members
Extension Methods

Remarks

This class enables using a socket to receive data that uses the HTTP protocol.

Constructors

HttpListener(string)

Creates an HTTP or HTTPS listener on the standard ports.

public HttpListener(string prefix)

Parameters

prefix string

Prefix ( http or https ) to start listen

Remarks

In the desktop version of .NET, the constructor for this class has no arguments.

HttpListener(string, int, IPAddress)

Creates an HTTP or HTTPS listener on the specified port.

public HttpListener(string prefix, int port, IPAddress localEndpointIP = null)

Parameters

prefix string

The prefix for the service, either "http" or "https".

port int

The port to start listening on. If -1, the default port is used (port 80 for http, or port 443 for https).

localEndpointIP IPAddress

The local endpoint to bind the socket to. If Null the default is used

Remarks

In the desktop version of .NET, the constructor for this class has no arguments.

Properties

HttpsCert

The certificate used if HttpListener implements an https server.

public X509Certificate HttpsCert { get; set; }

Property Value

X509Certificate

IsListening

Gets whether the HttpListener service was started and is waiting for client connections.

public bool IsListening { get; }

Property Value

bool

true if the HttpListener was started; otherwise, false.

MaximumResponseHeadersLength

Gets or sets the maximum allowed length of the response headers, in KB.

public int MaximumResponseHeadersLength { get; set; }

Property Value

int

The length, in kilobytes (1024 bytes), of the response headers.

Remarks

The length of the response header includes the response status line and any extra control characters that are received as part of the HTTP protocol. A value of -1 means no limit is imposed on the response headers; a value of 0 means that all requests fail. If this property is not explicitly set, it defaults to 4 (KB).

SslProtocols

Gets or sets the TLS/SSL protocol used by the HttpListener class.

public SslProtocols SslProtocols { get; set; }

Property Value

SslProtocols

One of the values defined in the SslProtocols enumeration.

Remarks

This property is specific to nanoFramework. There is no equivalent in the .NET API.

Methods

Abort()

Shuts down the HttpListener object immediately, discarding all currently queued requests.

public void Abort()

Remarks

This method disposes of all resources held by this listener. Any pending requests are unable to complete. To shut down the HttpListener object after processing currently queued requests, use the Close() method.

After calling this method, you will receive an ObjectDisposedException if you attempt to use this HttpListener.

Close()

Shuts down the HttpListener after processing all currently queued requests.

public void Close()

Remarks

After calling this method, you can no longer use the HttpListener object. To temporarily pause an HttpListener object, use the Stop() method.

GetContext()

Waits for an incoming request and returns when one is received.

public HttpListenerContext GetContext()

Returns

HttpListenerContext

An HttpListenerContext object that represents a client request.

Examples

This example shows how to call the GetContext method.

HttpListener myListener = new HttpListener("http", -1);
myListener.Start();
while (true)
{
HttpListenerResponse response = null;
try
{
Debug.Print("Waiting for requests");
HttpListenerContext context = myListener.GetContext();

Exceptions

SocketException

A socket call failed. Check the exception's ErrorCode property to determine the cause of the exception.

InvalidOperationException

This object has not been started or is currently stopped or The HttpListener does not have any Uniform Resource Identifier (URI) prefixes to respond to.

ObjectDisposedException

This object is closed.

Start()

Allows this instance to receive incoming requests.

public void Start()

Remarks

This method must be called before you call the GetContext() method. If the service was already started, the call has no effect. After you have started an HttpListener object, you can use the Stop() method to stop it.

Stop()

Causes this instance to stop receiving incoming requests.

public void Stop()

Remarks

If this instance is already stopped, calling this method has no effect.

After you have stopped an HttpListener object, you can use the Start() method to restart it.