HttpListener
HTTPS
C#
Networking
.NET

HttpListener class with HTTPS support

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

HttpListener can listen on HTTPS, but HTTPS support does not come from your C# code alone. Windows must already have an SSL certificate bound to the chosen port, and the process usually also needs the correct URL reservation, which is why most HttpListener HTTPS issues are configuration problems rather than API problems.

A minimal HTTPS listener

Once the OS-side setup exists, the C# side is simple:

csharp
1using System;
2using System.Net;
3using System.Text;
4using System.Threading.Tasks;
5
6class Program
7{
8    static async Task Main()
9    {
10        using var listener = new HttpListener();
11        listener.Prefixes.Add("https://localhost:8443/");
12        listener.Start();
13
14        Console.WriteLine("Listening on https://localhost:8443/");
15
16        while (true)
17        {
18            var context = await listener.GetContextAsync();
19            var responseText = "ok";
20            var buffer = Encoding.UTF8.GetBytes(responseText);
21
22            context.Response.ContentLength64 = buffer.Length;
23            await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
24            context.Response.Close();
25        }
26    }
27}

If HTTPS is configured correctly, this code can accept encrypted requests.

Bind a certificate with netsh

Before the listener can use HTTPS, Windows needs a certificate bound to the port:

powershell
netsh http add sslcert ipport=0.0.0.0:8443 certhash=YOUR_CERT_HASH appid={00112233-4455-6677-8899-AABBCCDDEEFF}

The important parts are:

  • 'ipport: the IP and port combination'
  • 'certhash: the certificate thumbprint'
  • 'appid: a GUID identifying the application'

Without this binding, HttpListener cannot terminate TLS on that port.

Reserve the URL if needed

Depending on how the process runs, you may also need a URL ACL:

powershell
netsh http add urlacl url=https://+:8443/ user=YOURMACHINE\\YourUser

This gives the process permission to listen on that prefix. Without it, HttpListener.Start() may fail even when the certificate binding exists.

Authentication and certificate expectations

The certificate used by HTTP.sys must already exist in a Windows certificate store that the system can access. The listener code does not load a .pfx file directly the way some other server stacks do. That is why HTTPS support with HttpListener feels different from app-level TLS configuration in Kestrel or reverse proxies.

If you need authentication, HttpListener can also be configured with built-in authentication schemes such as Basic, NTLM, or Negotiate. Those features are useful for Windows-centric internal tools, but they are another reason HttpListener is best suited to controlled environments rather than public internet services.

What HttpListener is good for

HttpListener is useful for:

  • local tools
  • internal utilities
  • lightweight Windows services
  • prototypes and controlled environments

For new public-facing services, ASP.NET Core with Kestrel is usually the better choice because it is more modern, more portable, and better supported for serious web hosting.

It is also more predictable across platforms. HttpListener is tightly tied to Windows HTTP.sys behavior, so even when the API looks small and convenient, the real deployment surface is larger than it first appears. That is usually the deciding factor for new projects and new teams today in production environments where supportability matters most over time.

Common Pitfalls

  • Expecting HTTPS to work without binding a certificate to the port first.
  • Forgetting the URL ACL reservation and blaming the C# code for a permission problem.
  • Using HttpListener as if it were the preferred server stack for a new production web application.
  • Debugging TLS in code when the real issue is Windows HTTP.sys configuration.

Summary

  • 'HttpListener supports HTTPS, but the OS must already have the certificate bound to the listening port.'
  • Use netsh http add sslcert for the certificate binding and urlacl when permissions require it.
  • Once the system configuration is correct, the C# listener code is relatively simple.
  • For new production services, Kestrel is usually a better long-term choice than HttpListener.

Course illustration
Course illustration

All Rights Reserved.