1

This is my first post, so please forgive me if I have made mistakes...

OK, what I need to do, is login to a website, and once logged in, I want to download a file. I prefer to be able to let the application run by itself on a daily basis.

So, on my first attempt, I used a webbrowser control, and used it with the DocumentCompleted method

        bool first = true;
    bool complete = false;
    private void wb1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (first)
        {
            //First part is to login to the site
            first = false;
            HtmlDocument doc = wb1.Document;
            HtmlElement username = doc.GetElementById("username");
            HtmlElement password = doc.GetElementById("password");
            HtmlElement submit = doc.GetElementById("submit");
            username.SetAttribute("value", "username");
            password.SetAttribute("value", "password");
            submit.InvokeMember("click");
        }
        else
        {
            //Next, I want to be able to download a file, using a url
            string fileUrl = "https://somebodys_logged_inwebsiteite.com/theFileNeeded.csv";
            string fName = @"C:\Path\file.csv";
            if (System.IO.File.Exists(fName))
            {
                System.IO.File.Delete(fName);
            }
            /*

             Once I am logged in, the first is now set to false
             Now I want to be able to download a file
             I failed in my attempt to use a WebClient 
            WebClient client = new WebClient();
            client.DownloadFileAsync(fileUrl, filepath);
             Because, when I set the client, I think it expects a login, as the csv file had login code at the bottom, and didnt contain the expected data, it was html code


             */

        }
    }

Can you help me to be able to download the file without having to use a saveasdialogue, because I want to run this on a task scheduler.

Thanks.

apomene
  • 14,282
  • 9
  • 46
  • 72
Mark
  • 11
  • 3

0 Answers0