Ultimately, I want to block downloads in .NET WebBrowser control, effectively restricting it to displaying HTML, images, scripts and the like, but never, ever display a \
Perhaps an open source proxy?
http://www.mentalis.org/soft/projects/proxy/
HttpListener
should be fine but it's just wrapper around http.sys and this library is available only on Windows XP and higher.
You only need one prefix http://127.0.0.1:8080/
because it's just for your local webcontrol. Alternatively, wildcards are supported like http://*:8080/
but there is no reason to use it in your case.
Jens Bannmann wrote:
The applications that are accessed are not on
localhost
, they can be anywhere. That's why I don't want to hard-code anything.
what do you mean by applications? you mean websites? this is something completely else, your special proxy server will listen for HttpListenerRequests on http://127.0.0.1:8080/
, and therefore your webcontrol has to use proxy server http://127.0.0.1:8080/
. It's still all within local machine at this point.
Convert each incoming HttpListenerRequest
into HttpWebRequest
, ask for response and you'll get HttpWebResponse
object, analyze it whether it's allowed response for your WebBrowser control and if it is, write it into HttpListnererResponse
object otherwise write in something else (error status).
This is probably the easiest way to build your own proxy server on .NET
Jens Bannmann wrote:
Right, this conversion was the thing I wanted to avoid having to do. Or can I do that in only a few code lines? From looking at the API, it looks more complicated.
It's actually quite easy because http protocol is trivial. It has basically three parts.
HttpListenerRequest
into HttpWebRequest
. Both classes have generic Headers property for raw access)Whole conversion would 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 assistence about http protocol, refer to this wikipedia article.
You can also check this open source project for another way of doing it. It doesn't depend on HttpListener
class but it's complete proxy server solution and it should be easy to modify for your needs.