1

Windows 7 SP1.
Domain network.
.NET Framework 4.6.1.

All my Internet browsers have configured proxy settings for Internet connections (it works fine).

I need to download file from Internet. I configure WebClient for it will read proxy settings from default Internet browser and use credential of current process and I expected these conditions are enough for successfull downloading. But I get the exception (look the comment in my code):

static void Main(string[] args) {
    String file_name = Path.GetRandomFileName();
    String full_path = Environment.ExpandEnvironmentVariables(
        Path.Combine(@"%LocalAppData%\Temp", file_name));

    using (WebClient client = new WebClient()) {
        client.Credentials = CredentialCache.DefaultCredentials;
        //client.Proxy = WebRequest.GetSystemWebProxy();
        var proxyUri = WebRequest.GetSystemWebProxy()
            .GetProxy(new Uri("https://yadi.sk/i/jPScGsw9qiSXU"));

        try {                    
            client.DownloadFile(proxyUri, full_path);
        }
        catch (Exception ex) {
            // The remote server returned an error: (502) Bad Gateway.
            Console.WriteLine(ex.Message);
        }
    }
    Console.WriteLine("Press any key for exit.");
    Console.ReadKey();
}

What I did wrong?

enter image description here

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

1 Answers1

1

You need to retrieve the proxy for the specific URL then set it as the proxy URL of the web request.

static void Main(string[] args) {
    String file_name = Path.GetRandomFileName();
    String full_path = Environment.ExpandEnvironmentVariables(
        Path.Combine(@"%LocalAppData%\Temp", file_name));

    using (WebClient client = new WebClient()) {
        client.Credentials = CredentialCache.DefaultCredentials;
        var proxyUri = WebRequest.GetSystemWebProxy()
            .GetProxy(new Uri("https://yadi.sk/i/jPScGsw9qiSXU"));
        client.Proxy = new WebProxy(proxyUri);
        client.Proxy.Credentials = CredentialCache.DefaultCredentials;

        try {                    
            client.DownloadFile("https://yadi.sk/i/jPScGsw9qiSXU", full_path);
        }
        catch (Exception ex) {
            // The remote server returned an error: (502) Bad Gateway.
            Console.WriteLine(ex.Message);
        }
    }
    Console.WriteLine("Press any key for exit.");
    Console.ReadKey();
}

This is implemented just in case the proxy uri is different depending on the url you are trying to access.

Gabriel Sadaka
  • 1,748
  • 1
  • 15
  • 19
  • Thank you, but I got the same problem again. – Andrey Bushman Apr 04 '16 at 09:22
  • `proxyUri` is instance of `Uri`, but `client.Proxy` is instance of `IWebProxy`. – Andrey Bushman Apr 04 '16 at 09:30
  • Also now I got other exception: *The remote server returned an error: (502) Bad Gateway.* :((( – Andrey Bushman Apr 04 '16 at 09:34
  • Please see latest update, you need to set the proxy uri as the proxy of the client. – Gabriel Sadaka Apr 04 '16 at 09:51
  • For your variant I get error: *The remote server returned an error: (407) Proxy Authentication Required.* Why do you use "https://yadi.sk/i/jPScGsw9qiSXU" string twice? – Andrey Bushman Apr 04 '16 at 09:55
  • You can store it in a variable up top. The GetProxy method will get the system proxy url based on the url you are trying to access. This can differ in certain proxy configurations e.g. accessing internal sites generally bypass proxies. Can you please add a breakpoint just before the DownloadFile call and confirm the proxyUri is what is set in IE? – Gabriel Sadaka Apr 04 '16 at 09:59
  • Please see latest update, you need to set the credentials of the proxy to the default credentials. – Gabriel Sadaka Apr 04 '16 at 10:18