Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The remote server returned error: (407) Proxy Authentication Required

I use this code with .NET 3.5 and receive error "The remote server returned an error: (407) Proxy Authentication Required."

using (WebClient client = new WebClient())
{
    WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

    try
    {
        string webPageStr = client.DownloadString(URL);
        Console.WriteLine("OK");
    }
    catch (Exception ex)
    {
        Console.WriteLine("FAIL");
        Console.WriteLine(ex.Message);
    }
}

However, this code works smoothly with .NET 4.0 as this line is sufficient to pass the proxy authentication while it is not for .NET 3.5.

WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

Therefore, I tried many other ways to solve this problem but none of them works:

1) Replace CredentialCache.DefaultCredentials line with

WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(user, password, domain);

2) Create new proxy object

IWebProxy proxy = new WebProxy(proxyUrl, port);
proxy.Credentials = new NetworkCredential(user, pass, domain);
client.Proxy = proxy;
client.Credentials = new NetworkCredential(user, pass, domain);

3) Add this line

client.UseDefaultCredentials = true;

4) Use HttpWebRequest instead of WebClient and repeat every procedure above. This is sample code.

HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential(user, pass, domain);
webRequest.Proxy.Credentials = new NetworkCredential(user, pass, domain);

try
{
    webRequest.GetResponse();
    Console.WriteLine("OK");
}
catch (Exception ex)
{
    Console.WriteLine("FAIL");
    Console.WriteLine(ex.Message);
}

I feel like I come to a dead end as I have to use .NET 3.5. There must be difference between these two .NET versions that I do not know. Thank you very much in advance.

like image 762
user3624964 Avatar asked May 11 '14 08:05

user3624964


People also ask

What does error code 407 mean?

The HTTP 407 Proxy Authentication Required client error status response code indicates that the request has not been applied because it lacks valid authentication credentials for a proxy server that is between the browser and the server that can access the requested resource.

What is proxy authentication required?

What Does Proxy Authentication Required Mean? The 407 Proxy Authentication Required error code indicates that the server cannot complete the request because the client lacks appropriate authentication credentials for a proxy server that intercepts the request between the client and server.


2 Answers

Just add this to config

 <system.net>
      <defaultProxy useDefaultCredentials="true" >
      </defaultProxy>
   </system.net>
like image 79
Ali Sarshogh Avatar answered Oct 09 '22 14:10

Ali Sarshogh


I've had this issue with Visual Studio solutions before. This helped me:

Open IE. Go to Tools -> Internet Options. Click on the Connections tab, then the LAN Settings button. Uncheck the "Automatically detect settings".

like image 30
Tony Avatar answered Oct 09 '22 14:10

Tony