Httpwebrequest Webexcepton The Remote Server Returned An Error 407 Proxy Authentication Required
- 24 Mar 2009
Problem
<a title=”System.Net Tracing” href=”http://msdn.microsoft.com/en-us/library/ty48b824.aspx” target=_blank mce_href=”http://msdn.microsoft.com/en-us/library/ty48b824.aspx”>System.Net Tracing</a> (see <a title=”My Favorite System.Net Tracing File” href=”http://blogs.msdn.com/jpsanders/archive/2009/03/24/my-favorite-system-net-trace-configuration-file-dumps-process-id-and-date-time-information.aspx” target=_blank mce_href=”http://blogs.msdn.com/jpsanders/archive/2009/03/24/my-favorite-system-net-trace-configuration-file-dumps-process-id-and-date-time-information.aspx”>My Favorite System.Net Tracing File</a>) revealed that we are getting the 407 error and the .NET framework is not retrying the request with Credentials. <p mce_keep="true">System.Net Error: 0 : [4811] Exception in the HttpWebRequest#33574938:: – The remote server returned an error: (407) Proxy Authentication Required. </p>
We can see in a <a title=Fiddler2 href=”http://www.fiddler2.com/” target=_blank mce_href=”http://www.fiddler2.com”>Fiddler2</a> 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<p mce_keep="true">By default the Proxy object does not supply the credentials of the Currently Logged on User. You can fix this by:</p> <p mce_keep="true">1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this:</p>
Copy Code:<div style="BACKGROUND-COLOR: #e0e0e0" id=proxyauthcodesection1>
<?xml:namespace prefix = o />
// 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 <a title=property href="http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx" target=\_blank mce\_href="http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx">property</a> to the <