How to implement your own proxy server?

Ultimately, I want to block the download in the .NET WebBrowser control , effectively restricting it to display HTML, images, scripts, etc., but never, displays the Save As dialog for the user.

The solution proposed in response to this question was to connect your own proxy server, which checks the answers and filters out everything that will lead to the download prompt. How can I implement such a proxy in my .NET application? I do not want to install third-party software or libraries.

I found the HttpListener class , but I have two problems:

  • The listener requires a predefined set of prefixes that trigger it. However, I would prefer not to record host names or port numbers in my application and not keep it shared.
  • I would need to implement a code that executes the request itself - is there something that does this for me, where I can just touch the line, parse the contents of the response and change it as necessary (for example, the query filter in J2EE does server side)?

Update

Well, it seems to me that I need to make this clearer: the .NET (rich client) application is used in several projects that also have web applications. The .NET application includes a reusable generic form with a WebBrowser control. Other developers use this form to integrate access to their web applications into a .NET application.

I want to block the download, so I want my WebBrowser form to intercept all traffic and ensure that this does not lead to the Save As dialog. Thus,...

  • I do not know what host names will be
  • WebBrowser points to real URLs, the user clicks on regular links, runs JavaScript ... everything that is served by the web application
  • If WebBrowser points to localhost (as suggested), I will have to parse the responses and rewrite all the links so that they point to localhost again, keeping the original URL. I do not want to go through this trouble.
+2
browser filter proxy response
source share
2 answers

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.

+5
source share
0
source share

All Articles