Home > database >  HttpListener works on Windows 10 but not Windows 11
HttpListener works on Windows 10 but not Windows 11

Time:09-15

I have an app which has been working fine under Windows 10, but since upgrading to Windows 11 has stopped working.

I've traced the problem to the HttpListener:

            listener.Prefixes.Add("http://" running_PC ":9123/");

            while (true)
            {

                listener.Start();
             
                // Note: The GetContext method blocks while waiting for a request.
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;
                
                HttpListenerResponse response = context.Response;
                
                string responseString = "AUTHORIZED";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                
                response.ContentLength64 = buffer.Length;
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                
                output.Close();
                listener.Stop();
             
            }

The specifics of it aren't really important (it basically replies "AUTHORIZED" to anything it receives). On Windows 10 it works fine. On Windows 11, it never gets past the GetContext() (which is not asynchronous - i.e. it blocks while waiting for an incoming request).

I've added the permission using netsh (x's are the IP address):

netsh http add urlacl url=http://xxx.xxx.xxx.xxx:9123/ user=Andrew

... but nothing. I've also checked firewalls etc.

Has anything changed between Win 10 and 11 please? Particularly on http etc, there's sometimes changes re security (e.g. suddenly https only unless you tweak something). I can't see anything in the documentation for the HttpListener that suggests this.

Does anyone have any ideas please?

CodePudding user response:

After further investigation, for some reason on Windows 11 you need to create a specific inbound rule in Windows Firewall to permit inbound packets on (in this case) port 9123. This is true even if there's a rule specifically for the receiving app (allow all), and true even if you're using a third party firewall (e.g. Comodo). Also, when specifying the inbound rule, you need to select allow even if insecure (i.e. not https).

So ... to answer my question no reason why a HttpListener shouldn't work in Win 11 - you just need to tweak the firewall differently for some reason.

  • Related