3

How to open socket thru proxy server in .Net C#?

So I opened up a socket on my machin. there are no nats between pe and proxy server. I connected to proxy server. How now to make Requests from global IP world that know that proxy server adrress be redirected or transfered by proxy server to me?

Any libs blog articles? Please help

Rella
  • 65,003
  • 109
  • 363
  • 636

1 Answers1

8

You cannot open a socket 'thru' something, only 'to' something. With proxy server (I assume you're talking about HTTP proxy that supports 'CONNECT' command) it's the same: first open a connection to it, then use its protocol to make proxy forward your connection where you want to using 'CONNECT' command.

So you need to do the following steps:

  1. Connect to proxy.
  2. Issue CONNECT Host:Port HTTP/1.1<CR><LF>
  3. Issue <CR><LF>
  4. Wait for a line of response. If it contains HTTP/1.X 200, the connection is successful.
  5. Read further lines of response until you receive an empty line.
  6. Now, you are connected to the outside world through a proxy. Do any data exchange you want.
Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • So I opened up a socket on my machin. there are no nats between pe and proxy server. I connected to proxy server. How now to make Requests from global IP world that know that proxy server adrress be redirected or transfered by proxy server to me? – Rella Jun 27 '10 at 11:22
  • Well, when at step 2 you say CONNECT ..., you're opening a connection to some remote host. You connect to it, not vice versa. And when you're connected - step 6 - on the remote side there's an opened socket that remote side can use to send data to you. – Haspemulator Jun 27 '10 at 11:31
  • 1
    can you send CONNECT with `` or you must send 2 packets? – SSpoke Jul 30 '15 at 07:25
  • Please this solution is not clear at all. Apparently several similar questions have been asked despite this answer. case in point http://stackoverflow.com/q/32909149/44080 Please A relevant solution with sample code would help. – Charles Okwuagwu Oct 05 '15 at 13:46
  • This was exactly what I needed. I found it to be very clear. – Tom Bascom Aug 01 '16 at 14:50
  • I posted our implementation here: https://stackoverflow.com/questions/35066981/how-to-use-proxy-with-tcpclient-connectasync/61953143#61953143 – Rychu May 22 '20 at 10:41
  • Also, it's best to set the Host when doing the Connect, I needed that for the proxy behind the proxy – Jordy Nov 21 '22 at 07:54