My computer is behind a router that forwards port 80 to it (as port 80). My problem is that HttpListener appears to ignore requests addressed to the external IP address.
The problem is not with the router; if I create a blank C# console project and add only the following lines of code:
System.Net.Sockets.TcpListener s = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 80);
s.Start();
System.Net.Sockets.TcpClient c = s.AcceptTcpClient();
...and run the program [note: the first time this program is run, the user may have to allow a firewall exception], it will terminate if I navigate to
http://localhost/testpath/
in my browser OR if I navigate to
http://externalipaddress/testpath/
in my browser -- either address successfully attempts a port 80 connection. So, everything is set up properly -- this computer can accept internal and external connections on port 80.
Now, if I create another blank C# console project and add only the following lines of code:
System.Net.HttpListener s = new System.Net.HttpListener();
s.Prefixes.Add("http://+:80/testpath/");
s.Start();
System.Net.HttpListenerContext c = s.GetContext();
...first, I'll get a HttpListenerException saying Access is denied because of the issues mentioned in this question. Then it will run properly when I close Visual Studio and reopen it as Administrator. The program will terminate (properly) when I navigate to
http://localhost/testpath/
However, my request simply times out if I navigate to
http://externalipaddress/testpath/
Why does my external request to HttpListener time out? The result is reproducible with a numeric IP address or domain name for 'externalipaddress', as well as many permutations of subfolders, slashes, etc, and also for the prefixes "http://*:80/testpath/", "http: //externaldomain:80/testpath/", and "http: //externalipaddress:80/testpath/" (no spaces; added for stackoverflow formatting).
It turns out the Windows firewall was silently eating the incoming port 80 requests from outside of localhost. When I disabled the firewall, the HttpListener detected the external requests. When I re-enabled the firewall, the HttpListener stopped detecting the external requests. When I enabled the disabled Inbound Rule entitled "BranchCache Content Retrieval (HTTP-In)" which allows the SYSTEM program (which hosts the service that does the TCP listening for HttpListener) to listen on port 80, the HttpListener again detected the external requests.
To find the Inbound Rules list on Windows 7, Start -> Control Panel -> System and Security -> Windows Firewall -> Advanced Settings -> Inbound Rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With