2

I'm trying to download a document with this code :

 using (Impersonator impersonator = new Impersonator())
 {
     siteURL += "?NoRedirect=true";
     System.Net.WebClient _WebClient = new System.Net.WebClient();
     // Downloads the resource with the specified URI to a local file.
     _WebClient.DownloadFile(siteURL, saveName);
 }

But i get an error 401 unothaurized access. Do you have any idea where it come froms ? Thanks

  • But what if credentialcache is empty at all? Suppose IIS set to anonymous access to the site. I'm getting this error when turning on anonymous access and disabling integrated security. So response come with 401 error. What I'm doing wriong? – user9061 Jun 23 '12 at 21:08
4

Try adding:

_WebClient.Credentials = CredentialCache.DefaultCredentials;
  • Thanks a lot, It works. Do you know if there is a way to know the path to the folder "My Download" in c# ? Thanks again – Thoma Bigueres Jun 4 '12 at 13:54
  • My downloads is not a SpecialFolder so cannot be accessed like for instance My documents using Environment.SpecialFolder. You can however get to that folder using the KnownFolders of the windows API, see the accepted answer for this question on SO: stackoverflow.com/questions/7672774/… – Colin Jun 23 '12 at 22:01
  • +1 for a great answer, I forgot all about the credentials. – keni Jan 14 '13 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.