HttpListener should be fine, but it's just a wrapper around http.sys, and this library is only available on Windows XP and above.
HttpPrefixes
You only need one prefix http://127.0.0.1:8080/ , because it is just for your local webcontrol. In addition, wildcards are supported as http://*:8080/ , but there is no reason to use it in your case.
Jens Bannmann wrote:
Access to applications is not on localhost , they can be anywhere. Thatβs why I donβt want to record anything.
what do you mean by applications? do you mean websites this is completely different, your special proxy server will listen to HttpListenerRequests at http://127.0.0.1:8080/ , and therefore your web control should use the proxy server http://127.0.0.1:8080/ . At this point, it is still inside the local machine.
Convert between HttpListenerRequest / Response and HttpWebRequest / Response
Convert each incoming HttpListenerRequest to HttpWebRequest , ask for an answer, and you will get an HttpWebResponse object, analyze it if it allowed a response for your WebBrowser control, and if so, write it to the HttpListnererResponse object otherwise write something else (error status).
This is probably the easiest way to create your own .NET proxy.
Jens Bannmann wrote:
That's right, this transformation was what I wanted to avoid having to do. Or can I do this in just a few lines of code? From looking at the API, it looks more complex.
This is actually quite simple because the HTTP protocol is trivial. It consists mainly of three parts.
- Request string (contains URL, http-method and http-version).
- Headers (this is actually what makes the API so huge and important, but in fact all these properties and methods are just a thin layer on top of the raw http headers. All you have to do is copy all the headers directly from
HttpListenerRequest to HttpWebRequest . Both classes have a common header for accessing cheese) - The body of the message (just copy its contents, if any)
The whole conversion will look something like this:
HttpListenerRequest listenerRequest; WebRequest webRequest = WebRequest.Create(listenerRequest.Url); webRequest.Method = listenerRequest.HttpMethod; webRequest.Headers.Add(listenerRequest.Headers); byte[] body = new byte[listenerRequest.InputStream.Length]; listenerRequest.InputStream.Read(body, 0, body.Length); webRequest.GetRequestStream().Write(body, 0, body.Length); WebResponse webResponse = webRequest.GetResponse();
If you need more help with the HTTP protocol, see this wikipedia article .
You can also check out this open source project for another way to do this. It does not depend on the HttpListener class, but it is fully responsible for the proxy server and it needs to be easily modified for your needs.