.NET HttpListener: when registering both HTTP & HTTPS I get “conflicts with an existing registration on the machine”

StackOverflow https://stackoverflow.com/questions/2619251

  •  26-09-2019
  •  | 
  •  

Question

I'm trying to use .NET HttpListener in a C# project. When I register my prefix "http://*:8080/" it does not seem to work for HTTPS urls (i.e. doesn't pick them up). When I try the following code to register both the HTTP and HTTPS versions of the prefix however I get the error:

"Failed to listen on prefix 'https://:8080/' because it conflicts with an existing registration on the machine."*

How can I get my prefix working for both HTTP & HTTPS?

    private HttpListener _listener;

    // Create prefixes
    var prefixes = new List<string>();
    prefixes.Add("http://*:8080/");
    prefixes.Add("https://*:8080/");


    // Create HttpListener
    _listener = new HttpListener();
    foreach (string prefix in prefixes)
    {
        _listener.Prefixes.Add(prefix);
    }

    _listener.Start();   // <== ERROR HERE

EDIT 1 - Additional clarification:

  • The program is working as a local proxy for PC applications making HTTP(S) calls out.
  • Usage is therefore based on changing one's browser proxy settings to point to this local home grown proxy server (e.g. localhost:8080)
  • This implies therefore (I assume) that the HttpListener has to listen for both HTTP and HTTPS traffic on this same local port (e.g. 8080).
  • OBJECTIVE: Try to find a way for my program to listen to both HTTP and HTTPS on the same port.

thanks

Was it helpful?

Solution

OBJECTIVE: Try to find a way for my program to listen to both HTTP and HTTPS on the same port.

You can't do this with HTTPListener. You'll need to use a TCPListener and handle each response conditionally depending on whether it is HTTP or HTTPS.

I'm pretty sure if you use one HTTPListener for HTTP 8080 and one for HTTPS 8443 you should be able to make your browser use yourproxy:8080 for HTTP and yourproxy:8443 for HTTPS. Firefox definitely let's you do this.

OTHER TIPS

By default, https using the port 443 rather than 80. You have to specify different port to different protocol lister.

This sounds non-trivial but not impossible. You will need to inspect what the client sends and decide if they are starting an HTTP or HTTPS session based on the data that they supply.

edit: Thinking about this a little more this is probably not what you want to do given that you are writing a proxy. Instead you will need to handle the CONNECT method (see http://www.ietf.org/rfc/rfc2817.txt) and open up a tunneled connection to the target server.

"If you create an HttpListener using https, you must select a Server Certificate for that listener. Otherwise, an HttpWebRequest query of this HttpListener will fail with an unexpected close of the connection." This is from msdn site. (http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx) Maybe problem is there?

You are not forced to use the same config for all the protocols.

Hit advanced in the proxy settings, and there you can specify different proxy settings for other protocols.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top