C#WebClient或Web浏览器

时间:2018-06-29 14:42:27

标签: c# save

这是我的第一篇文章,所以如果我犯了错误,请原谅我...

好的,我需要做的是登录一个网站,登录后,我想下载一个文件。我希望能够每天让应用程序自行运行。

因此,我第一次尝试使用Web浏览器控件,并将其与DocumentCompleted方法一起使用

        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


             */

        }
    }

您能帮助我无需使用saveasdogue来下载文件吗,因为我想在任务计划程序上运行它。

谢谢。

0 个答案:

没有答案