I am using a web client class in my source code for downloading a string using http.

This was working fine. However, the clients in the company are all connected now to a proxy server. And the problem started from this.

When I have tested my application I don't think it can pass through the proxy server, as the exception that keeps getting thrown is "no response from xxx.xxx.xxx.xxx which is the proxy server IP address.

However, I can still navigate to the web site URL and it displays the string correctly in the browser when connecting through a proxy server, but not when I use my web client.

Is there something in the web client that I have to configure to allow me to access the url from behind a proxy server?

using (WebClient wc = new WebClient())
    string strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxx";

    //Download only when the webclient is not busy.
    if (!wc.IsBusy)
        string rtn_msg = string.Empty;
            rtn_msg = wc.DownloadString(new Uri(strURL));
            return rtn_msg;
        catch (WebException ex)
            return false;
        catch (Exception ex)
            return false;
        System.Windows.Forms.MessageBox.Show("Busy please try again");
        return false;

My solution:

WebClient client = new WebClient();
WebProxy wp = new WebProxy(" proxy server url here");
client.Proxy = wp;
string str = client.DownloadString("http://www.google.com");
  • 4
    Ideally, WebClient should also be in a using statement as it implements IDisposable. Useful post though. – Orphid Jan 4 '15 at 12:53
  • 1
    To be more plainer than Orphid; with the above code (which is perfectly legitimate) don't forget to call .Dispose on the webclient! Also, WebProxy has a constructor overload that takes a port number as a second argument – user3791372 Jan 18 '17 at 4:43

If you need to authenticate to the proxy, you need to set UseDefaultCredentials to false, and set the proxy Credentials.

WebProxy proxy = new WebProxy();
proxy.Address = new Uri("mywebproxyserver.com");
proxy.Credentials = new NetworkCredential("usernameHere", "pa****rdHere");  //These can be replaced by user input
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;  //still use the proxy for local addresses

WebClient client = new WebClient();
client.Proxy = proxy;

string doc = client.DownloadString("http://www.google.com/");

If all you need is a simple proxy, you skip most of the lines above though. All you need is:

WebProxy proxy = new WebProxy("mywebproxyserver.com");

I've encountered the same issue but using a webclient for downloading a file from the internet with a Winform application the solution was adding in the app.config:

    <defaultProxy useDefaultCredentials="true" />

The same solution will work for an asp.net app inserting the same rows in web.config.

Hope it will help.


The answer proposed by Jonathan is proper, but requires that you specify the proxy credentials and url in the code. Usually, it is better to allow usage of the credentials as setup in the system by default (Users typically configure LAN Settings anyway in case they use a proxy)...

The below answer has been provided by Davide in earlier answer, but that requires modifying the app.config files. This solution is probably more useful since it does the same thing IN CODE.

In order to let the application use the default proxy settings as used in the user's system, one can use the following code:

IWebProxy wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials; 
wc.Proxy = wp;

This will allow the application code to use the proxy (with logged-in credentials and default proxy url settings)... No headaches! :)

Hope this helps future viewers of this page to solve their problem!

  • 1
    How come I don't have to do this when running Fiddler (local proxy client)? All calls channel through fiddler when using WebClient, even without configuring proxy. – Nilzor Dec 2 '14 at 6:53

You need to configure the proxy in the WebClient object.

See the WebClient.Proxy property:


byte[] data;
using (WebClient client = new WebClient())
    ICredentials cred;
    cred = new NetworkCredential("xmen@test.com", "mybestpassword");
    client.Proxy = new WebProxy("",8000);
    client.Credentials = cred;
    string myurl="http://mytestsite.com/source.jpg";
    data = client.DownloadData(myUrl);

File.WriteAllBytes(@"c:\images\target.jpg", data);

Your Answer

