How to create a simple proxy in C#?

I have downloaded Privoxy few weeks ago and for the fun I was curious to know how a simple version of it can be done.

I understand that I need to configure the browser (client) to send request to the proxy. The proxy send the request to the web (let say it's a http proxy). The proxy will receive the answer... but how can the proxy send back the request to the browser (client)?

I have search on the web for C# and http proxy but haven't found something that let me understand how it works behind the scene correctly. (I believe I do not want a reverse proxy but I am not sure).

Does any of you have some explication or some information that will let me continue this small project?

Update

This is what I understand (see graphic below).

Step 1 I configure the client (browser) for all request to be send to 127.0.0.1 at the port the Proxy listen. This way, request will be not sent to the Internet directly but will be processed by the proxy.

Step2 The proxy see a new connexion, read the HTTP header and see the request he must executes. He executes the request.

Step3 The proxy receive an answer from the request. Now he must send the answer from the web to the client but how???

替代文字

Useful link

Mentalis Proxy : I have found this project that is a proxy (but more that I would like). I might check the source but I really wanted something basic to understand more the concept.

ASP Proxy : I might be able to get some information over here too.

Request reflector : This is a simple example.

Here is a Git Hub Repository with a Simple Http Proxy.


您可以使用HttpListener类来构建一个侦听传入请求并使用HttpWebRequest类来中继请求。


I wouldn't use HttpListener or something like that, in that way you'll come across so many issues.

Most importantly it'll be a huge pain to support:

  • Proxy Keep-Alives
  • SSL won't work (in a correct way, you'll get popups)
  • .NET libraries strictly follows RFCs which causes some requests to fail (even though IE, FF and any other browser in the world will work.)
  • What you need to do is:

  • Listen a TCP port
  • Parse the browser request
  • Extract Host connect to that host in TCP level
  • Forward everything back and forth unless you want to add custom headers etc.
  • I wrote 2 different HTTP proxies in .NET with different requirements and I can tell you that this is the best way to do it.

    Mentalis doing this, but their code is "delegate spaghetti", worse than GoTo :)


    Proxy can work in the following way.

    Step1, configure client to use proxyHost:proxyPort.

    Proxy is a TCP server that is listening on proxyHost:proxyPort. Browser opens connection with Proxy and sends Http request. Proxy parses this request and tries to detect "Host" header. This header will tell Proxy where to open connection.

    Step 2: Proxy opens connection to the address specified in the "Host" header. Then it sends HTTP request to that remote server. Reads response.

    Step 3: After response is read from remote HTTP server, Proxy sends the response through an earlier opened TCP connection with browser.

    Schematically it will look like this:

    Browser                            Proxy                     HTTP server
      Open TCP connection  
      Send HTTP request  ----------->                       
                                     Read HTTP header
                                     detect Host header
                                     Send request to HTTP ----------->
                                     Server
                                                          <-----------
                                     Read response and send
                       <-----------  it back to the browser
    Render content
    
    链接地址: http://www.djcxy.com/p/62488.html

    上一篇: 使用套接字通过代理将请求从客户端转发到服务器c#

    下一篇: 如何在C#中创建一个简单的代理?