1

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!

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
gman
  • 11
  • 2

1 Answers1

0

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.

Safdar Akrami
  • 245
  • 1
  • 3
  • 12
  • Thanks Safdar! The username and password I inputed worked for me when I access the site via web browser. Maybe to refine my question, given that I have the correct credential and I am able to enter into the site and download files via web browser, I would like to be able to write a code that can pass the credentials to the site and automate the download for me. – gman Jan 27 '17 at 15:12