I had some code in an application which I work on which uses Excel to open a .csv file from a URL. The problem is that user’s have moved to Excel 2010 (yes we’re a little behind the latest versions) and basic authentication is no longer supported without registry changes (see Office file types fail to open from server).
So, to re-implement this I needed to write some code to handle the file download myself (as we’re no able to change user’s registry settings).
The code is simple enough , but I thought it’d be useful to document it here anyway
WebClient client = new WebClient(); client.Proxy = WebRequest.DefaultWebProxy; client.Credentials = new NetworkCredential(userName, password); client.DownloadFile(url, filename);
This code assumes that the url is supplied to this code along with a filename for where to save the downloaded file.
We use a proxy, hence the proxy is supplied, and then we supply the NetworkCredential which will handle basic authentication. Here we need to supply the userName and password, ofcourse with basic authentication these will be passed as plain text over the wire.