views:

2339

answers:

4

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

+6  A: 

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
Completely useless comment...Library by Benton is fine.
@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
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.
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
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...
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
+2  A: 

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.

Greg
+1  A: 

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

Sean
How can I use that one in conjunction with TcpClient?
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
Unfortunately the code in that article works with `Socket`, not `TcpClient`.
Drew Noakes
+7  A: 

I have a free MIT licensed, open source proxy library. You can download it from my site www.starksoft.com or from Sourceforge. It is easy to use, works directly with the standard TcpClient object in .NET and supports multiple configurations and async calls. Works with HTTP, Socks4, Socks4a, and Socks5 proxy servers and comes with integrated help. It also works with Mono. Enjoy!

http://www.starksoft.com/prod_proxy.html

http://sourceforge.net/projects/starksoftproxyc/

Benton

Thank you. It looks fantastic.
@benton, does it support socks 5 authentication?
Sumit Ghosh
Looks like a good solution, thanks. The first link seems to be dead, though but the sf.net project has a code/binary download available.
Drew Noakes