Eric Sven

Eric Sven

  • NA
  • 9
  • 650

Log in and download file

Apr 14 2020 10:54 AM
Hello Everyone,

I'm trying to write a C# program that can login and download a file from a website.

Here is my code:
  1. using System;  
  2. using System.Collections.Specialized;  
  3. using System.Net;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace CodeProjectDownload  
  7. {  
  8.     public class CookiesAwareWebClient : WebClient  
  9.     {  
  10.         public CookieContainer CookieContainer;  
  11.         public CookieContainer MyProperty  
  12.         {  
  13.             get { return CookieContainer; }  
  14.             set { CookieContainer = value; }  
  15.         }  
  16.         public CookiesAwareWebClient()  
  17.         {  
  18.             CookieContainer = new CookieContainer();  
  19.         }  
  20.   
  21.         protected override WebRequest GetWebRequest(Uri address)  
  22.         {  
  23.             WebRequest request = base.GetWebRequest(address);  
  24.             ((HttpWebRequest)request).CookieContainer = CookieContainer;  
  25.             return request;  
  26.         }  
  27.     }   
  28.   
  29. public class Program  
  30. {  
  31.     private static void Download(string email, string password, string destination, string downloadUri)  
  32. {  
  33.     using (CookiesAwareWebClient client = new CookiesAwareWebClient())  
  34.     {  
  35.         NameValueCollection values = new NameValueCollection();  
  36.         values.Add("Email",email );  
  37.         values.Add("Password", password);  
  38.         values.Add("x""10");   
  39.         values.Add("y""10");   
  40.         values.Add("login""login");  
  41.   
  42.         // We authenticate first  
  43.         client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);  
  44.   
  45.         // Now we can download  
  46.   
  47.         //string modifieduri = "http://www.codeproject.com/script/articles/download.aspx?file=" + downloadUri.Replace("http://www.codeproject.com","") + "&rp=";  
  48.     string modifieduri = "https://www.codeproject.com/KB/buttons/1087610/FontAwesomeImageSample.zip";  
  49.     Console.WriteLine("url " + modifieduri);  
  50.   
  51.         string filename = System.IO.Path.GetFileName(downloadUri);  
  52.   
  53.         string filepathname = System.IO.Path.Combine(destination, filename);  
  54.   
  55.         client.DownloadFile(modifieduri, filepathname);  
  56.     }  
  57.   
  58. }  
  59.         static void Main(string[] args)  
  60.         {  
  61.     try  
  62.     {  
  63.             Download("[email protected]""mfAwItJZ9+xZ", @"C:\Users\lswennen\Work Folders\WIP\Archiving\BIMCo connection""/KB/buttons/1087610/FontAwesomeImageSample.zip");  
  64.             //Console.ReadLine();  
  65.             MessageBox.Show("ok");   
  66.     } catch (Exception e)  
  67.       {  
  68.                 Console.WriteLine("{0}", e);  
  69.         MessageBox.Show("pas ok");  
  70.       }  
  71.     }  
  72. }  
  73. }  
And when I run it, a "zip file" is downloaded but "corrupted". When I open it with the notepad, it looks like a html source code. So I added ".html" extension" and when I open it it shows the webpage which will start the downloading. If I wait a bit my browser try to download the file but it fails because it's from my ".html" file.

What am I doing wrong to download the html source code and not the file ?

Thanks in advance.

Answers (1)