The remote server responded with an error: (407) Proxy authentication required

I use this code with .NET 3.5 and get the error message "The remote server responded with 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, since this line is enough for proxy authentication, and not for .NET 3.5.

 WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials; 

So I tried many other ways to solve this problem, but none of them work:

1) Replace the line CredentialCache.DefaultCredentials with

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

2) Create a 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 each procedure described 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); } 

It seems to me that I am at a dead end, since I need to use .NET 3.5. There should be a difference between the two versions of .NET that I don't know. Thank you so much in advance.

+6
source share
3 answers

Just add this to your configuration

  <system.net> <defaultProxy useDefaultCredentials="true" > </defaultProxy> </system.net> 
+10
source

I had this problem with Visual Studio solutions before. It helps me:

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

0
source

Sometimes restarting Visual Studio solves this problem.

0
source

All Articles