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:


Copy Code:



                    // 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:


Copy Code:






<system.net>
    <defaultProxy useDefaultCredentials=”true”> </defaultProxy>
</system.net>

Comments (7)

  1. Oracy says:

    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".

  2. Dave says:

    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.

  3. Aravind Mohan says:

    This worked for me… I used this for a forms application and it works awesome 🙂

    Thank you jeff

  4. Calum says:

    4 years on and still helpful for a project i'm working on, thanks worked a treat

  5. rafiq ahamed says:

    this soles my issue . thanks a lot

  6. AA says:

    it solved my issue too…thanks a ton….

  7. RP Singh says:

    Really good code, solves my prob. Thanks a lot…………………