使用c#daily basis

时间:2016-11-17 09:47:52

标签: c# selenium-webdriver download

我知道本网站上有很多关于我的主题的帖子,并且已经阅读了其中的一些内容,但一般都没有回答我的问题。

非常简单:我想要一个允许我从具有此约束的网站下载文件的解决方案:

  • 在下载文件之前,我需要在两个文本框中指定开始和结束日期,因此我无法直接使用URL,因为此URL将一直更改。

  • 此文件需要每天下载4次,无需人工干预。

所以最好的方法是使用scrpit,例如,使用Windows调度程序,此脚本在特定时间执行并下载文件。

我的问题是:使用c#的最佳技术是什么? 我没有很多c#的经验......从我读到的内容来看,Selenium Framework对WebDriven有好处。我可以使用它并自动下载吗? 因为根据我的阅读,用户需要运行他的项目,但它可以像脚本一样自动化......我是对的吗?

或者知道另一种方法吗?

1 个答案:

答案 0 :(得分:0)

我无法测试这个,因为我的工作机器上没有C#,但我认为它会是这样的。

以下是一个涵盖这些选项的示例:

WebClient client = new WebClient();
DateTime yesterday = DateTime.Today.AddDays(-1);
DateTime tomorrow = DateTime.Today.AddDays(1);
Uri ur = new Uri(http://remotehost.do/images/" + yesterday + "to" + tomorrow + "/excel.xls");
client.Credentials = new NetworkCredential("username", "password");
client.DownloadProgressChanged += WebClientDownloadProgressChanged;
client.DownloadDataCompleted += WebClientDownloadCompleted;
client.DownloadFileAsync(ur, @"C:\path\excel.xls");

回调函数的实现如下:

void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("Download status: {0}%.", e.ProgressPercentage);
}

void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    Console.WriteLine("Download finished!");
}