WebProxy error: Proxy Authentication Required

37,595

Solution 1

You've to set the WebClient.Proxy Property..

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

Solution 2

This worked for me:

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

Solution 3

Try this code

var transferProxy = new WebProxy("localproxyIP:8080", true);
transferProxy.Credentials = new NetworkCredential("user", "password", "domain");
var transferRequest = WebRequest.Create("http://www.google.com");
transferRequest.Proxy = transferProxy;
HttpWebResponse transferResponse = 
    (HttpWebResponse)transferRequest.GetResponse(); 
System.IO.Stream outputStream = transferResponse.GetResponseStream();
Share:
37,595

Related videos on Youtube

Oleg  Ignatov
Author by

Oleg Ignatov

Updated on July 12, 2022

Comments

  • Oleg  Ignatov
    Oleg Ignatov almost 2 years

    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.

    • jwaliszko
      jwaliszko
      try to remove the domain\\ part (also remember of wrapping WebClient with using() statement because it implements IDisposable)
  • jwaliszko
    jwaliszko over 11 years
    it's not needed because WebRequest.DefaultWebProxy = p is enough
  • 2GDev
    2GDev over 11 years
    you're right if you use WebRequest.Create() and after you call WebRequest.GetResponse(). But in this case he's using the WebClient to make request so the Proxy is not the same.
  • jwaliszko
    jwaliszko over 11 years
    Actually DownloadString method internally uses WebRequest object created by WebRequest.Create(). If proxy wasn't particularly set to WebClient object, such WebRequest object uses proxy obtained from WebRequest.InternalDefaultWebProxy property, which is just set by WebRequest.DefaultWebProxy = p statement;
  • Brad Bruce
    Brad Bruce almost 11 years
    Wonderful! This should be part of every WebClient sample.
  • Peska
    Peska over 6 years
    Imho solution below stackoverflow.com/a/17187136/7225096 is better.
  • amartin94
    amartin94 almost 6 years
    Can confirm this answer is perfect - We distributed software to clients that kept hitting this issue due to network restrictions. This patched the single issue everyone was facing. This should be the selected answer. YOU SAVED ME
  • KevinBui
    KevinBui almost 4 years
    I use Console dotnet Core, how can i config it in setting file (such as app.config in Net Framework)

Related