'C# Download File from HTTP File Directory getting 401 error or 403 error

I’m trying to download several files from a local network device: http file directory

I want to write a code that will automatically download all those .avi files to my pc drive.

I have 2 problems:

Problem 1: AUTHENTICATING using WebClient class only. If I use WebClient class only to connect, I get a 401 Unauthorized error.

Code:

try
{
    using (WebClient myWebClient = new WebClient())
    { 
                    myWebClient.UseDefaultCredentials = false;
                    myWebClient.Credentials = new NetworkCredential("user", "pword");
                    String userName = "user";
                    String passWord = "pword";
                    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));
                    myWebClient.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
                    Console.WriteLine("Header AUTHORIZATION: "+ myWebClient.Headers[HttpRequestHeader.Authorization].ToString());

                // Download the Web resource and save it into the current filesystem folder.
                Console.WriteLine("Start DL");
                myWebClient.DownloadFile("http://192.168.2.72:81/sd/20170121/record000/P170121_000000_001006.avi", "P170121_000000_001006.avi");
                Console.WriteLine("End DL");
      }
}
catch(Exception ex)
{
                Console.WriteLine("DOWNLOAD ERROR: " + ex.ToString());
 }

Error Message: Failure to authenticate 401 Unauthorized Error

Problem 2: Was able to authenticate using WebProxy class but can’t download . Getting 403 Not found error.

Code:

try
{
    using (WebClient myWebClient = new WebClient())
    {
            WebProxy wp = new WebProxy("http://192.168.2.72:81/sd/20170121/record000/",false);                   
            wp.Credentials = new NetworkCredential("user","pword");
            Console.WriteLine("Web Proxy: " + wp.Address);
            myWebClient.UseDefaultCredentials = false;
            myWebClient.Credentials = wp.Credentials;
            myWebClient.Proxy = wp;
            Console.WriteLine("Downloading File \"{0}\" from \"{1}\"\n\n", filename, wp.Address);

            // Download the Web resource and save it into the current filesystem folder.
            Console.WriteLine("Start DL");
            myWebClient.DownloadFile("http://192.168.2.72:81/sd/20170121/record000/P170121_000000_001006.avi", "P170121_000000_001006.avi");
            Console.WriteLine("End DL");
        }
     }
     catch(Exception ex)
     {
         Console.WriteLine("DOWNLOAD ERROR: " + ex.ToString());
     }

Error Message: 403 Not Found

DOWNLOAD ERROR: System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at ConsoleApplication2.Program.Main(String[] args) in C:\Users\Gordon\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 139

Please help me identify if there are any mistakes in my code or is there a better way to submit credentials and download all the files.

Thanks in advance!



Solution 1:[1]

I'm not Dot Net developer, I'm just sharing my opinion. In the second point you have mentioned that you are getting 403 which is the Http status code for Acces Denied. I feel your credentials are not valid or you don't have privilege to do the operation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Safdar Akrami