Question

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

Basically, we're doing 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;
}

The proxy is one that we have configured ourselves using FreeProxy.

I've enabled logging and on the machine I'm testing with, and can confirm that requests are being made to the proxy when using it in Firefox.

No authentication is required for the proxy server, except that the IP has to be within our office (which from the Firefox evidence, I assume is not the problem).

However, within our background process, I don't seem to be using the proxy when I use the webclient:

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

I receive no errors from the proxy however, it seems like we're just going along without it even though the proxy has explicitly been set.

The information seems to return fine, just not through our proxy.

Is there something I'm missing when using a proxy with a WebClient?

edit: more details. If we disable the proxy service on the server, then we get an exception that we can't connect. So it seems like the webclient is attempting to reach out to the proxy, but that traffic is not actually flowing 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
Was it helpful?

Solution

It turns out that FreeProxy wasn't accepting HTTPS traffic.

I guess the proxy must return the types of traffic it can route, and if it cannot, the webclient does nothing.

Switched to using the Burp suite as our proxy since it can accept HTTPS.

http://portswigger.net/burp/

OTHER TIPS

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

You are using the WebClient class correctly as far as I can tell. I am able to see the following request...

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

in Fiddler running on my local box. Now if I shut Fiddler down, I get the WebException:

Unable to connect to the remote server

With an inner SocketException of:

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

So, with that said, my guess is that your proxy is working as intened but it is just not logging the outgoing HTTP request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top