Using C # WebClient with proxy - no request to proxy?

We have a background operation (Window service) that we want to use through a proxy server.

Basically, we do this:

public WebClient GetWebClient(){ var webClient = new WebClient(); webClient.proxy = new WebProxy(Configuration.ProxyHost, Configuration.ProxyPort); // add a bunch of headers to the WebClient (sessionids, etc.) return webClient; } 

A proxy server is one that we ourselves configured using FreeProxy .

I turned on logging on the machine I'm testing with, and I can confirm that requests are executed in proxies when used in Firefox.

Authentication is not required for the proxy server, except that the IP address must be in our office (which, according to Firefox, I do not consider a problem).

However, in our background process, I don't seem to use a proxy server when using the web client:

 using(var wc = GetWebClient()) using(var s = wc.OpenRead("someurl")) using(var sr = new StreamReader(s)){ return sr.ReadToEnd(); } 

I do not get any errors from the proxy server, but it looks like we just can do without it, even though the proxy is explicitly installed.

It seems that the information returned perfectly, just not through our proxy.

Is there something I am missing when using proxies with WebClient ?

edit: more details. If we disable the proxy service on the server, we will get an exception with which we cannot connect. Thus, it seems like the web client is trying to contact the proxy server, but this traffic does not actually flow through the proxy.

 Inner Exception: SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 
+4
source share
3 answers

It turns out FreeProxy did not accept HTTPS traffic.

I assume that the proxy should return the types of traffic that it can route, and if it cannot, the web client does nothing.

I switched to using the Burp package as our proxy server, since it can accept HTTPS.

http://portswigger.net/burp/

+1
source

You either need to configure windows to use the default proxy, or manually set the proxy in your code, see http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(v=VS.100 ) .aspx

0
source

You are using the WebClient class correctly, as far as I can tell. I can see the following query ...

 using(var client = new WebClient()) { client.Proxy = new WebProxy("localhost", 8888); Console.WriteLine(client.DownloadString("http://www.google.com")); } 

Fiddler works in my local field. Now, if I close Fiddler, I get a WebException:

 Unable to connect to the remote server 

With an internal SocketException:

 No connection could be made because the target machine actively refused it 127.0.0.1:8888 

So, with that said, I assume that your proxy works as intensively, but it just does not register the outgoing HTTP request.

0
source

All Articles