Using the following code:
HttpListener listener = new HttpListener();
//listener.Prefixes.Add("http://*:80/");
listener.Prefixes.Add("http://*:8080/");
listener.Prefixes.Add("http://*:8081/");
listener.Prefixes.Add("http://*:8082/");
listener.Start();
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
The program hangs on the GetContext();
despite loading http (not https) pages in IE and Firefox.
When I uncomment the first line I get the error:
Failed to listen on prefix 'http://*:80/' because it conflicts with an existing registration on the machine.
So how do I listen to a browser's requests?
It hangs because GetContext() is waiting for a request to be received, as said in its documentation:
For more info see: https://msdn.microsoft.com/en-us/library/system.net.httplistener.getcontext(v=vs.110).aspx
Using Asynchronous model tends to be complex, another alternative is running all that code in a different Thread but it depends on your goals.
Don't reinvent the wheel and just use the FiddlerCore
EDIT
You can start with this rectangular wheel :)
I would consider looking into this package http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OwinSelfHost/ It uses HttpListener under the covers and with the WebApi HttpMessageHandler it is very easy to create a proxy.
this port is probably being used... run netstat -ano on the command line, youll see list of the ports that are being used and the their process ids.
I dont know, why the
GetContext();
hangs, because there is too less information about what happens with the listerner variable, but the problem with port 80 usually is caused by Skype, because it uses port 80 by default. To fix that, open Skype's preferences, go to advanced->connection and uncheck "Use Port 80 and 443 as an alternative for incoming Connections".Replace
*
in your prefixes with+