How to implement custom proxy server?

后端 未结 2 1683
你的背包
你的背包 2021-02-04 22:56

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 \

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 23:24

    HttpListener should be fine but it's just wrapper around http.sys and this library is available only on Windows XP and higher.

    HttpPrefixes

    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.

    Converting between HttpListenerRequest/Response and HttpWebRequest/Response

    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.

    • Request line (it contains URL, http method and http version)
    • Headers (this is actually what makes API look so huge and important but in reality all those properties and methods are just thin layer over raw http headers. All you need to do is to copy all headers directly in generic way from HttpListenerRequest into HttpWebRequest. Both classes have generic Headers property for raw access)
    • Message body (just copy its content if there's any)

    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.

提交回复
热议问题