views:

361

answers:

3

Hi,

Background - I can get HttpListener working fine for HTTP traffic. I'm having trouble with HTTPS traffic however.

QUESTION: How can I change the code below so that a browser request to a "https" URL will actually be picked up by my HttpListener?

Notes - At the moment with firefox's proxy settings set to "localhost:8080", when I listen to traffic on port 8080 ("https://*:8080/"), and I enter a HTTPS url in firefox, I am getting no traffic being picked up? (when I listen to just http and enter normal http url's it works fine)

  _httpListener = new HttpListener();
  _httpListener.Prefixes.Add("https://*:8080/");
  _httpListener.Start();

thanks

A: 

It sounds like Firefox may not be proxying HTTPS traffic like you expect. Does your HTTPS request actually render in the browser (even though no traffic shows up in your HTTPListener)?

Make sure your Firefox browser is set up to proxy SSL to the same location as HTTP, or else make sure the "Use this proxy for all server protocols" box is checked.

Ken Taylor
the browser stays blank actually - I have got the firefox proxy setting set to "Use this proxy for all server protocols"
Greg
Ok, then the hang-up probably is at the listener, as Michael suggests. I have encountered certificate issues with .NET code before; when you have a cert problem, it tends to fail silently and leave you wondering what is going on. You definitely need a server-side cert in order to do SSL.
Ken Taylor
OK so just a normal server type SSL certificate, the kind you'd put on a website then? Can you produce one on an XP PC do you know? (i.e. not a Windows Server)
Greg
Yes, you can do a self-generated cert (Google "self-generated certificate" if you need help), and you will also need to make it a trusted cert on your machine (or else the .NET framework will block it on security grounds).
Ken Taylor
Thanks - do you know if these certificates + HttpListener be enough to handle what I'm trying to do? Or would I have to write my own code to getCertificate info, respond to Ssl setup request etc?
Greg
Yes, it should be. Doing a quick Google search turned up several walk-throughs that may help you; I like this one best:http://blogs.msdn.com/jpsanders/archive/2009/09/29/walkthrough-using-httplistener-as-an-ssl-simple-server.aspx
Ken Taylor
+2  A: 

From MSDN

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.

You can configure Server Certificates and other listener options by using HttpCfg.exe. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/http/http/httpcfg_exe.asp for more details. The executable is shipped with Windows Server 2003, or can be built from source code available in the Platform SDK.

Not sure 100% though...

Michael
interesting - I really want to just get the request and then "copy" it through to a new request (to act as a proxy itself). Anyone else able to confirm/deny Michael's thoughts?
Greg
it's not as simple as in http. your client(browser) will establish connection to the server using 443 port, then will retrieve certificate information. A request will look like this: CONNECT localorglobal.com:443 HTTP/1.0. Server then will respond with HTTP/1.0 200 Connection established and a binary encrypted data will be transferred between client and server.
Michael
Oh..ok..So could a separate HttpListener listening on port 443 handle this? It kind of seems the listener you build would need to know how to handle the certificates etc, or would HttpListener do this for you? In other words what would be the simplest way in .NET C# code to emulate what a webserver normally does to handle the SSL setup phase?
Greg
PS. Just noted a Web Server C# project a codeplex http://webserver.codeplex.com/ - perhaps the answers lay buried in some of this code?
Greg
No you can't listen on 443 and handle this, unless you implement SSL stack on your own :) You might also be interested to check this MSDN article: http://blogs.msdn.com/jpsanders/archive/2009/09/29/walkthrough-using-httplistener-as-an-ssl-simple-server.aspx
Michael
A: 

Check the FW settings if it passes that port at all.

Itay Levin