Skip to content

IPv6 System.Net.HttpListenerException (400): Invalid port in prefix. #34399

Description

@maxthedread

dotnet --version
3.1.201

_httpListener.Prefixes.Add("http://[::1]:54321/meow/");

works fine on Windows but fails on linux-x64 (WSL, Ubuntu)

exception: System.Net.HttpListenerException (400): Invalid port in prefix.
at System.Net.HttpEndPointManager.AddPrefixInternal(String p, HttpListener listener)
at System.Net.HttpEndPointManager.AddListener(HttpListener listener)
at System.Net.HttpListener.Start()

Additionally the behavior in regards of IPv4 IPv6 on Windows and Linux is different. With the prefix:
http://localhost:54321/meow/

HttpListener binds both to IPv4 and IPv6 on Windows. But not on Linux.

Activity

  1. davidsh commented on Apr 1, 2020

    @davidsh
    Contributor

    works fine on Windows but fails on linux-x64 (WSL, Ubuntu)

    FYI, the implementation of HttpListener on Linux is substantially different than Windows. Windows uses the original .NET Framework (ported to .NET Core) implemented layered on top of http.sys. Linux uses a separate implementation originally from Mono.

  2. added and removed
    untriagedNew issue has not been triaged by the area owner
    on May 14, 2020
  3. added this to the Future milestone on May 14, 2020
  4. josemoliver commented on Sep 21, 2022

    @josemoliver

    Similar issue here, works fine in Windows 11 (.net 6.0.401) but fails in Raspberry Pi OS (linux-arm 6.0.401)
    System.Net.HttpListenerException (400): Invalid path.

    listener.Prefixes.Add("http://*:"+port+"/"+path);

    https://github.com/josemoliver/WeatherServer/blob/main/Program.cs

  5. shapea commented on Feb 19, 2025

    @shapea

    This was reported nearly 5 years ago?

    The issue here is a clear bug in the .NET code under src/System/Net/Managed/HttpEndPointManager.cs in AddPrefixInternal. This method is used for all HttpListener prefixes on non-Windows hosts and uses very basic string parsing which isn't capable of handling IPv6 addresses in any form due to simply treating the first : character as the beginning of the port, which obviously doesn't work for IPv6 addresses which contain : characters.

    See:

  6. added
    in-prThere is an active PR which will close this issue when it is merged
    on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Netbugin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions