Why do I get (407) Proxy Authentication Required in a WinForms app and not a Console app?

1.7k Views Asked by At

I've got a problem getting my software to communicate through a proxy server at a client's site. It just gets (407) Proxy Authentication Required errors. It's a .NET Framework 3.5 C# WinForms application which uses old-style asmx web references to communicate (not WCF).

To diagnose the problem, I created a simple C# console app:

static void Main(string[] args)
{
    try
    {
        System.Net.WebClient client = new System.Net.WebClient();
        client.DownloadFile(@"https://www.myserver.co.uk/test.xml", @"C:\temp\test.xml");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

This gives the error (407) Proxy Authentication Required. So I added this to the app config:

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

And it works. Great! However, in a similarly simple test WinForms application where I've put the same code behind a button click, I still get the 407 error with or without the extra config.

What's the difference between a Console application and a WinForms application in which the latter doesn't seem to send the default credentials? What else can I do to diagnose this?

Other things I've tried:

  • Setting the credentials in code using CredentialCache.DefaultCredentials and CredentialCache.DefaultNetworkCredentials.

  • Creating a new WebProxy and setting the default credentials in code.

  • Changing the config to point to a non-existent web proxy. This gives a different error, which shows that the WinForms app is at least reading the config.

I haven't tried creating a new NetworkCredential with username and password in code, partly because this wouldn't really be a viable solution for the production app, but mainly because I don't know the password! I'm having to do all my diagnosis over LogMeIn because the client site is in a different country and I'm dealing with a less-than-helpful IT department.

Thanks for any help.

0

There are 0 best solutions below