Search code examples
c#authenticationproxycredentialswebrequest

HttpWebRequest doesnt not use default IE proxy with credentials


On Windows 7 I put a proxy in Internet Options settings. This proxy requires authentication that being asked in IE, then I put (and save) the credentials and then I'm able to access the web. But when I use this code I get WebException: (407) Proxy Authentication Required.

var request = WebRequest.Create(@"https://google.com");
var res = request.GetResponse();

I see in debug that the WebRequest is getting the default proxy uri and port but for some reason it doesn't do the authentication. neither from saved Windows Credentials nor by poping a window to ask for credentials.

I also tried to set it implicitly with this code and got the same 407:

var systemProxy = WebRequest.GetSystemWebProxy();
var systemProxyUri = systemProxy.GetProxy(new Uri("http://www.google.com"));
var sysWebProxy = new WebProxy(systemProxyUri) { UseDefaultCredentials = true };
WebRequest.DefaultWebProxy = sysWebProxy;

What do I miss? How can I define WebRequest to use the default system's proxy with the saved credentials? or at least make windows to ask for credentials?

I cant use the app.config and don't want to put credentials hard coded.


Solution

  • no it does not !

    if you code a programm, display a popup and ask for credentials

    IWebProxy proxy = WebRequest.DefaultWebProxy;
            proxy.Credentials = new NetworkCredential(proxyLogin.Text, proxyPassword.Password);