HttpListener Class

Provides a simple, programmable, cross-platform HTTP protocol listener.

Syntax

public class HttpListener : IDisposable

Remarks

The HttpListener class allows you to listen for incoming HTTP requests on a specific URI prefix. It provides a simple way to create custom HTTP servers without the need for a full web server implementation. You can use HttpListener to handle requests for web services, APIs, or any other HTTP-based communication.

To use HttpListener, you typically perform the following steps:

  1. Create an instance of the HttpListener class.
  2. Add one or more URI prefixes to the HttpListener.Prefixes collection.
  3. Call the Start() method to begin listening for requests.
  4. Call the GetContext() method to wait for and retrieve an incoming request. This method blocks until a request is received.
  5. Process the request using the HttpListenerContext object returned by GetContext().
  6. Send a response back to the client using the HttpListenerResponse object from the context.
  7. Repeat steps 4-6 to handle multiple requests.
  8. Call the Stop() method when you are finished listening.

Example

C# Example

using System;
using System.Net;
using System.Text;

public class SimpleHttpServer
{
    public static void Main(string[] args)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("HttpListener is not supported on this platform.");
            return;
        }

        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes.
        listener.Prefixes.Add("http://localhost:8080/");
        listener.Prefixes.Add("http://127.0.0.1:8080/");

        listener.Start();
        Console.WriteLine("Listening on http://localhost:8080/");

        try
        {
            while (true)
            {
                // Note: The GetContext method blocks while waiting for a request.
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;

                Console.WriteLine($"Received request: {request.HttpMethod} {request.Url}");

                string responseString = $"

Hello from HttpListener!

You requested: {request.Url}

"; byte[] buffer = Encoding.UTF8.GetBytes(responseString); HttpListenerResponse response = context.Response; response.ContentLength64 = buffer.Length; response.ContentType = "text/html"; response.OutputStream.Write(buffer, 0, buffer.Length); response.Close(); } } catch (HttpListenerException ex) { Console.WriteLine($"HttpListenerException: {ex.Message}"); } finally { listener.Stop(); Console.WriteLine("Server stopped."); } } }

Methods

Start()

Begins listening for incoming HTTP requests.

public void Start()

Stop()

Stops listening for incoming HTTP requests.

public void Stop()

GetContext()

Waits for an incoming request and returns an HttpListenerContext object that represents the request and the response.

public HttpListenerContext GetContext()

Properties

Prefixes

Gets a collection of URI prefixes that the HttpListener listens to.

public HttpListenerPrefixCollection Prefixes { get; }

IsListening

Gets a value that indicates whether the HttpListener object is currently listening for requests.

public bool IsListening { get; }

Important

When using HttpListener, you need to have the necessary permissions to bind to the specified URI prefixes. On Windows, this typically requires administrator privileges or configuring URL reservations using netsh.exe.