Using C# WebClient with a Proxy - no request made to proxy server?

ddango picture ddango · Oct 20, 2011 · Viewed 8.4k times · Source

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

Answer

ddango picture ddango · Oct 27, 2011

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/