7

How can I communicate through an HTTP proxy with TcpClient in C#, kind of like WebProxy when using HttpWebResponse?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

3 Answers3

8

Well, TCP doesn't have anything directly equivalent to HTTP proxying. In HTTP, the client (generally) knows about the proxying - it talks to the proxy, and asks the proxy to connect to the real web server on its behalf.

TCP doesn't define that sort of thing, so any proxying would have to either be transparent (i.e. something that a router or the operating system does without the client knowing, e.g. with iptables) or as part of the protocol on top of TCP (HTTP proxying is a good example of this, as is SOCKS mentioned in a different answer).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    @conker: I dare say the library Benton linked to is fine if you're using the kind of proxy server mentioned in his answer. That doesn't disagree with any part of my answer though. – Jon Skeet May 25 '09 at 16:19
  • 2
    The question was simple, all other answers in this thread are to the point. Nobody was asking or answering about proxy server, especially not "that" or "other" "kind" of proxy server. It was about implementing proxy client on top of TCPClient. Stop talking our of your ass about things you have no idea about. – user21582 May 26 '09 at 20:56
  • 1
    Well, I'm still pretty convinced that I *do* know what I'm talking about, and see no reason to delete this answer. If no-one was asking about any kind of proxy server, I do wonder what on earth anyone's going to talk to... there has to be *something* doing the proxying, and it's going to have to talk *some* appropriate protocol, isn't it? – Jon Skeet May 26 '09 at 21:03
  • Your answer is not really helpful no matter how you look at it. Compare it with others, one is going into need of custom implementation on top of sockets (which is mostly correct), second gives link to partial solution, third is giving the most complete solution . Ironically, your answer gets the most votes and full solution gets no votes... – user21582 May 27 '09 at 10:07
  • 2
    Um, every answer here has a vote. It looks like people disagree with you about whether or not it's "not really helpful no matter how you look at it". I suspect we'll just have to agree to disagree. – Jon Skeet May 27 '09 at 14:05
3

If you go down to low-level socket programming, I'm pretty sure you'll need to write your own proxy client. If you're only dealing with the HTTP protocol, you're probably better off using HTTP-specific classes. If you need to do it with sockets, the HTTP spec describes the behavior of proxies reasonably well, so you could write your own client.

Community
  • 1
  • 1
Greg
  • 10,360
  • 6
  • 44
  • 67
1

If you'd like to use a SOCKS proxy, there are already some SOCKS libraries written for C#. Try this one.

Sean
  • 401
  • 3
  • 7
  • How can I use that one in conjunction with TcpClient? –  Mar 27 '09 at 22:44
  • Perhaps subclassing TcpClient would be best, but I don't think they're compatible. TcpClient is somewhat rigid in its usage, so it's not that flexible. – Sean Mar 28 '09 at 02:44
  • Unfortunately the code in that article works with `Socket`, not `TcpClient`. – Drew Noakes Sep 05 '10 at 20:35