HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.
Problem
System.Net Tracing (see My Favorite System.Net Tracing File) revealed that we are getting the 407 error and the .NET framework is not retrying the request with Credentials.
System.Net Error: 0 : [4811] Exception in the HttpWebRequest#33574938:: - The remote server returned an error: (407) Proxy Authentication Required.
We can see in a Fiddler2 trace that Internet Explorer does submit the 407 credentials of the currently logged on user, but the HttpWebRequest does not even try. We also see that for HTTP traffic there is no 407 in either request. Only HTTPS traffic was requiring credentials for the proxy.
Resolution
By default the Proxy object does not supply the credentials of the Currently Logged on User. You can fix this by:
1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this:
// Begin code change by jeff
// Obtain the 'Proxy' of the Default browser.
IWebProxy theProxy = aReq.Proxy;
// Print the Proxy Url to the console.
if (theProxy != null)
{
// Use the default credentials of the logged on user.
theProxy.Credentials = CredentialCache.DefaultCredentials;
}
// End code change by jeff
HttpWebResponse aResp = aReq.GetResponse() as HttpWebResponse;
2. Add this property to the <<application>>.exe.config or machine.config file:
<system.net>
<defaultProxy useDefaultCredentials="true"> </defaultProxy>
</system.net>
Comments
Anonymous
December 03, 2009
I'm having this kind of problem and cannot find a way to get authenticated. I've tried your solution, but it didn't helped either. By then way, the property above is wrong. It's missing a quote rigth after then word "true".Anonymous
November 11, 2010
I've searched hi and low for this - tried adding specific proxy information etc but all I needed to do was set the credentials. "By default the Proxy object does not supply the credentials of the Currently Logged on User" - was the key piece of information for me.Anonymous
November 20, 2011
This worked for me... I used this for a forms application and it works awesome :) Thank you jeffAnonymous
November 27, 2013
4 years on and still helpful for a project i'm working on, thanks worked a treatAnonymous
January 22, 2015
this soles my issue . thanks a lotAnonymous
March 30, 2015
it solved my issue too...thanks a ton....Anonymous
August 24, 2015
Really good code, solves my prob. Thanks a lot.....................