views:

13096

answers:

5
+1  A: 

You can build one with the HttpListener class to listen for incoming requests and the HttpWebRequest class to relay the requests.

Mark Cidade
Where do I relay? How can I know where to send back the information? The browser send to lets said 127.0.0.1:9999 the client at 9999 get the request and sent it to the web. Get an answer... THAN what the client do? Send to what address?
Daok
If you're using HttpListener, you just write the response to HttpListener.GetContext().Response.OutputStream. No need to care for the address.
OregonGhost
Interesting, I'll check in this way.
Daok
I wouldn't use HttpListener for this. Instead, build an ASP.NET app and host it within IIS. When using HttpListener, you're giving up the process model provided by IIS. This means you lose things like process management (startup, failure detection, recycling), thread pool management,etc.
Mauricio Scheffer
See http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
Mauricio Scheffer
That is, if you intend to use it for many client computers... for a toy proxy HttpListener is ok...
Mauricio Scheffer
A: 

The browser is connected to the proxy so the data that the proxy gets from the web server is just sent via the same connection that the browser initiated to the proxy.

Stephen Caldwell
+5  A: 

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
Vadmyst
+4  A: 

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 :)

dr. evil
A: 

Agree to dr evil if you use HTTPListener you will have many problems, you have to parse requests and will be engaged to headers and ...

1.Use tcp listener to listen to browser requests
2.parse only the first line of the request and get the host domain and port to connect 3.send the exact raw request to the found host on the first line of browser request 4.receive the data from the target site(I have problem in this section) 5.send the exact data received from the host to the browser

you see you dont need to even know what is in the browser request and parse it, only get the target site address from the first line first line usually likes this GET http://google.com HTTP1.1 or CONNECT facebook.com:443 (this is for ssl requests)

Alireza Rinan