Is there any way to use a socks proxy with WebClient
? Specifically with the DownloadString
method that it provides?
I don't want to use any third party stuff like privoxy, freecap whatever and I can't use commercial libraries like those from Chilkat. I tried using stuff from http://www.mentalis.org/ in fact I used their WebRequest
implementation but they don't seem to have something similar for WebClient.
SOCKS is not supported directly by the
WebRequest
/WebResponse
classes and by extension, the WebClient class (it relies onWebRequest
to do its work).It really can't, as it works on the transport layer (TCP/IP) and not through a simple redirect to a server that forwards HTTP requests (which is the level that the
WebRequest
/WebResponse
classes work on).You can create a specialized derivation of
WebRequest
/WebResponse
(that usesProxySocket
to handle the low-level handshaking and then) and then create a specializedWebClient
class which overrides theGetWebRequest
andGetWebResponse
methods.Once you have that class substituted for your
WebClient
instances, it should work as normal (you might have to set up the proxy in each case where you use it though).Here is how I ended up doing it, thank you casperOne for the answer
The
SocksHttpWebRequest
class is taken from the blog linked to by @casperOne, the code for which is as follows:Note that, as @casperOne pointed out, this makes use of a (free) third party library called ProxySocket.
I came across this aswell and found the nice BetterHttpClient
It derives from WebClient and allows you to specify a socks proxy:
Here is how I ended up doing it, thank you casperOne for the answer
I'm looking for the same code but it doesnt really work for me. I wonder where you've got the
IProxyDetails
-Object. It seems not implemented in blog and not in the files i got from there