0% found this document useful (0 votes)
307 views

Net - How To Create A Simple Proxy in C#? - Stack Overflow

The document discusses how to create a simple proxy in C#. It explains that a proxy works by configuring the client browser to send requests to the proxy instead of directly to the internet. The proxy then sends the request to the intended web server, receives the response, and sends it back to the client browser. Several methods for implementing this in C# are discussed, including using HttpListener, TCPListener with TcpClient, or building an ASP.NET application hosted on IIS.

Uploaded by

Atif Toor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views

Net - How To Create A Simple Proxy in C#? - Stack Overflow

The document discusses how to create a simple proxy in C#. It explains that a proxy works by configuring the client browser to send requests to the proxy instead of directly to the internet. The proxy then sends the request to the intended web server, receives the response, and sends it back to the client browser. Several methods for implementing this in C# are discussed, including using HttpListener, TCPListener with TcpClient, or building an ASP.NET application hosted on IIS.

Uploaded by

Atif Toor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

.net - How to create a simple proxy in C#?

- Stack Overflow 18/07/2019, 4+06 PM

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.
135 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
80 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 connection, 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???

https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 1 of 6
.net - How to create a simple proxy in C#? - Stack Overflow 18/07/2019, 4+06 PM

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.

c# .net .net-2.0 proxy

edited Jun 3 '18 at 7:21 community wiki


10 revs, 4 users 97%
Patrick Desjardins

I do not have a screenshot of 2008 in 2015. Sorry. – Patrick Desjardins Jul 29 '15 at 20:19

Actually, it turns out that archive.org does have it. Sorry to bother you. – Ilmari Karonen Jul 29 '15 at
20:29

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
9ourAnswers
Terms of Service.

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

33 answered Oct 22 '08 at 17:34


Mark Cidade
86.4k 29 209 226

Where do I relay? How can I know where to send back the information? The browser send to lets
https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 2 of 6
.net - How to create a simple proxy in C#? - Stack Overflow 18/07/2019, 4+06 PM

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? – Patrick Desjardins Oct 22 '08 at 17:39

2 If you're using HttpListener, you just write the response to


HttpListener.GetContext().Response.OutputStream. No need to care for the address. –
OregonGhost Oct 22 '08 at 18:11

7 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 Jan 15 '09 at 23:07

2 That is, if you intend to use it for many client computers... for a toy proxy HttpListener is ok... –
Mauricio Scheffer Jan 15 '09 at 23:12

2 both methods are now obsolete... Any updates? – michaeljiz Sep 16 '15 at 22:33

I wouldn't use HttpListener or something like that, in that way you'll come across so many
issues.
85 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 :)

answered Apr 30 '09 at 11:05 community wiki


dr. evil

1 What class(es) did you use for the TCP connections? – Cameron Jan 17 '11 at 0:03

8 @cameron TCPListener and SslStream. – dr. evil Jan 17 '11 at 9:05

1 Excellent, thanks! – Cameron Jan 17 '11 at 16:46

https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 3 of 6
.net - How to create a simple proxy in C#? - Stack Overflow 18/07/2019, 4+06 PM

2 Could you please share you expirience on why HTTPS won't work? – Restuta Aug 25 '11 at 21:50

10 @Restuta for SSL to work you should forward connection without actually touching it in TCP level
and HttpListener can't do that. You can read how SSL works and you'll see it require to authenticate
to the target server. So client will try to connect to google.com but actually will connect your
Httplistener which is not google.com and will get a cert mismatch error and since your listener won't
be using signed cert, will get incorrect cert etc. You can fix it by installing a CA to computer that client
will use though. It's a pretty dirty solution. – dr. evil Aug 26 '11 at 18:44

I have recently written a light weight proxy in c# .net using TcpListener and TcpClient.

https://github.com/titanium007/Titanium-Web-Proxy
21
It supports secure HTTP the correct way, client machine needs to trust root certificate used by
the proxy. Also supports WebSockets relay. All features of HTTP 1.1 are supported except
pipelining. Pipelining is not used by most modern browsers anyway. Also supports windows
authentication (plain, digest).

You can hook up your application by referencing the project and then see and modify all traffic.
(Request and response).

As far as performance, I have tested it on my machine and works without any noticeable
delay.

edited Dec 21 '18 at 15:12 community wiki


6 revs
justcoding121

Proxy can work in the following way.

Step1, configure client to use proxyHost:proxyPort.


19
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

https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 4 of 6
.net - How to create a simple proxy in C#? - Stack Overflow 18/07/2019, 4+06 PM

detect Host header


Send request to HTTP ----------->
Server
<-----------
Read response and send
<----------- it back to the browser
Render content

answered Apr 30 '09 at 10:55 community wiki


Vadym Stetsiak

If you are just looking to intercept the traffic, you could use the fiddler core to create a proxy...

http://fiddler.wikidot.com/fiddlercore
13
run fiddler first with the UI to see what it does, it is a proxy that allows you to debug the
http/https traffic. It is written in c# and has a core which you can build into your own
applications.

Keep in mind FiddlerCore is not free for commercial applications.

edited Oct 20 '14 at 16:11 community wiki


2 revs
Dean North

Things have become really easy with OWIN and WebAPI. In my search for a C# Proxy server,
I also came across this post http://blog.kloud.com.au/2013/11/24/do-it-yourself-web-api-proxy/
6 . This will be the road I'm taking.

answered Jan 19 '15 at 12:34 community wiki


Jochen van Wylick

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 ...
5 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

https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 5 of 6
.net - How to create a simple proxy in C#? - Stack Overflow 18/07/2019, 4+06 PM

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)

edited Oct 13 '14 at 18:12 community wiki


3 revs, 2 users 87%
Alireza Rinan

Socks4 is a very simple protocol to implement. You listen for the initial connection, connect to
the host/port that was requested by the client, send the success code to the client then
4 forward the outgoing and incoming streams across sockets.

If you go with HTTP you'll have to read and possibly set/remove some HTTP headers so that's
a little more work.

If I remember correctly, SSL will work across HTTP and Socks proxies. For a HTTP proxy you
implement the CONNECT verb, which works much like the socks4 as described above, then
the client opens the SSL connection across the proxied tcp stream.

answered Jun 15 '12 at 15:20 community wiki


C.M.

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.
2 answered Nov 3 '08 at 16:15 community wiki
Stephen Caldwell

https://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c Page 6 of 6

You might also like