WebProxy error: Proxy Authentication Required

Oleg  Ignatov picture Oleg Ignatov · Oct 22, 2012 · Viewed 35.9k times · Source

I use the following code to obtaing html data from the internet:

WebProxy p = new WebProxy("localproxyIP:8080", true);
p.Credentials = new NetworkCredential("domain\\user", "password");
WebRequest.DefaultWebProxy = p;
WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.google.com");

But the following error is appeared: "Proxy Authentication Required". I can't use default proxy because of my code runs from windows service under the special account which there is no default proxy settings for. So, I want to specidy all proxy settings in my code. Please advice me how to resolve this error.

Answer

Johan Larsson picture Johan Larsson · Jun 19, 2013

This worked for me:

IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
client = new WebClient
    {
        Proxy = defaultWebProxy
    };
string downloadString = client.DownloadString(...);