11

How do I use WebClient.DownloadFile with digest authentication and a query string?

When I try to use it I get a 401 response.

This is the Apache error log:

[Tue Jun 24 17:31:49 2014] [error] [client x.x.x.x] Digest: uri mismatch - </file-1.php> does not match request-uri </file-1.php?since=1403587422>

Here is how I try to download the file:

Uri uri = new Uri("http://example.com/file-1.php?since=1403587422");
WebClient webClient = new WebClient();
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(
  new Uri(uri.GetLeftPart(UriPartial.Authority)),
  "Digest",
  new NetworkCredential("username", "password")
);
webClient.Credentials = credentialCache;
webClient.DownloadFile(uri, file.OutputFile);
Petah
  • 45,477
  • 28
  • 157
  • 213
  • Perhaps http://stackoverflow.com/questions/3172510/how-can-i-do-digest-authentication-with-httpwebrequest – Brian Jun 27 '14 at 03:46
  • You are supposed to get a 401 response after the request. The 401 response contains the WWW-Authenticate header that you need to calculate the challenge response. Check out how the digest authentication protocol works. http://technet.microsoft.com/en-us/library/cc780170(v=ws.10).aspx – Mike Hixson Jul 01 '14 at 04:58
  • @MikeHixson I get the 401 fine, its the request after that which fails. – Petah Jul 02 '14 at 00:21
  • Try the workaround in the below link – Deepak Mishra Jul 02 '14 at 11:01
  • just for debug purpose hard-code the url on the credentialCache "http : //example.com" and see if anything changes. – Pedro.The.Kid Jul 03 '14 at 15:17
  • @Petah Maybe this will help? http://httpd.apache.org/docs/2.0/mod/mod_auth_digest.html Try adding this to your httpd.conf BrowserMatch "MSIE" AuthDigestEnableQueryStringHack=On – ErocM Mar 26 '15 at 15:54
  • @Petah This is where I found it originally: https://www.drupal.org/node/128962 – ErocM Mar 26 '15 at 15:55

1 Answers1

2
WebClient webCl = new WebClient();
webCl.Credentials = new NetworkCredential("username", "Password");
webCl.DownloadFile(file download URL, fil save path with FileName);
Kanishka
  • 267
  • 4
  • 21