locked
"The remote server returned an error: (401) Unauthorized" RRS feed

  • Question

  • Hi,

    I have a console application which will help to download URL file using the System.Net.WebClient. I am having two site to download the file using username and password.  using the below code i am able to download the file from one site however, i am not able to download the URL file from the second site, its trowing the exception "The remote server returned an error: (401) Unauthorized". i am providing correct credentials ( I have checked manually to down load file using the credentials). Using .net framewok version 4.6. Below is the sample code which i am using.

     using (System.Net.WebClient client = new System.Net.WebClient())
    {                                      
     client.Credentials = new System.Net.NetworkCredential(usr, pw);
     client.DownloadFile(downloadURL, filePath);

    }

    Any help on this would be appriciate

    Thursday, October 6, 2016 3:44 PM

Answers

  • Hi Christoper,

    Thanks for your help.

    I am not the owner of the server. i don't have access of that server. I am agree with your answer. However, i am able to download files manually using the credentials.

    Regards,

    Sajjaya Kumar


    Obviously, the credentials your application is presenting are invalid. You need to have the owner of the server to tell just what are the credentials that are being rejected, which can be checked in the logon audit logs that can be enabled on the O/S that will show logon credentials that are successful or not successful.  
    Monday, October 10, 2016 2:54 PM

All replies

  • Hello Sajjaya,

    Not sure from the post but take a look at some of these posts.  Others were affected by the operating system they were running on while others had success by sending the credentials in a credential cache instead:

    http://stackoverflow.com/questions/2150161/webclient-generates-401-unauthorized-error

    http://stackoverflow.com/questions/26275903/webclient-the-remote-server-returned-an-error-401-unauthorized

    Using webrequest/webresponse instead:

    http://stackoverflow.com/questions/5207660/401-unauthorized-returned-on-get-request-https-with-correct-credentials  


    Cheers, Jeff

    Thursday, October 6, 2016 7:15 PM
  • Hi Sajjaya,

    You can try adding the user agent header to your code as shown below:

    using (System.Net.WebClient client = new System.Net.WebClient())
    {
       client.Credentials = new System.Net.NetworkCredential(usr, pw);
       client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
       client.DownloadFile(downloadURL, filePath);
    }


    Regards, Arcee Gomes

    Friday, October 7, 2016 5:09 AM
  • Hi Jeff,

    Thanks for your help. But it is not working for me.

    Friday, October 7, 2016 6:35 PM
  • Hi Arcee,

    Thanks for you help.

    This is also not working for me. I am using the .net framework 4.6. I have seen lot of people facing this issue. however, no help.

    Regards,

    Sajjaya Kumar

    Friday, October 7, 2016 6:38 PM
  • Hi Arcee,

    Thanks for you help.

    This is also not working for me. I am using the .net framework 4.6. I have seen lot of people facing this issue. however, no help.

    Regards,

    Sajjaya Kumar

    If both the Credentials and Spoofed User agent string are correct, then we can not help you.

    That message is being send by the target server. It can decide what to Authorize or not entirely arbitrarily.

    If you are the owner of that server, make sure it is set to accept message from beyond localhost/the local network. Many serves are limited like this by default.
    If you are not the owner of the that server, ask the owner of that server. He somehow screwed up the configuration/documentation horribly or does only like certain browser to use that page.

    This sounds like a Networking problem, not a programming one.
    Generally the Network related classes do not care if the target is on the same computer, the same switch or on the Voyager 2 Probe. Making certain there is a path and that the server accepts the connection is a networking/adminsitration problem.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Friday, October 7, 2016 8:17 PM
  • Hi Christoper,

    Thanks for your help.

    I am not the owner of the server. i don't have access of that server. I am agree with your answer. However, i am able to download files manually using the credentials.

    Regards,

    Sajjaya Kumar

    Monday, October 10, 2016 1:53 PM
  • Hi Christoper,

    Thanks for your help.

    I am not the owner of the server. i don't have access of that server. I am agree with your answer. However, i am able to download files manually using the credentials.

    Regards,

    Sajjaya Kumar


    Obviously, the credentials your application is presenting are invalid. You need to have the owner of the server to tell just what are the credentials that are being rejected, which can be checked in the logon audit logs that can be enabled on the O/S that will show logon credentials that are successful or not successful.  
    Monday, October 10, 2016 2:54 PM
  • OK, Thanks :)
    Monday, October 10, 2016 3:28 PM
  • Hi Sajjaya Kumar,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help members if they faces similar issue.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, October 17, 2016 2:31 PM
  • //Download file
                    WebClient webClient = new WebClient();
                    Uri address = new Uri(url);
                    webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                    webClient.UseDefaultCredentials = false;
                    webClient.DownloadFile(address, fileName);
    Adding the 'user-agent' header fixed my issue of a 401 unauthorized error when trying to download a file from an url. Thank you for posting!
    • Edited by boardforces Thursday, January 17, 2019 10:02 PM
    Thursday, January 17, 2019 10:01 PM