DownloadStringAsync维护订单的多个URL

时间:2013-10-12 19:43:53

标签: c# html url asynchronous

我需要将多个HTML页面下载为字符串并将它们存储到列表中。但随后需要按照我开始下载的顺序存储。目前,列表按下载完成顺序排列。因此,当下载完成时,其内容将添加到列表中。但他们不一定按照他们开始的顺序完成。我有点难以理解如何按照发布顺序将这些数据存储在列表中。

这是我当前的代码,这将启动下载序列。 URLList是一个网址列表,我需要下载的字符串与原始列表的顺序相同:

cts = new CancellationTokenSource();
try
{
    await AccessTheWebAsync(cts.Token);
    listBox1.DataSource = VideoList;
}
catch (Exception)
{
    MessageBox.Show("\r\nDownload failed.\r\n");
}
cts = null;

然后调用以下代码:

async Task AccessTheWebAsync(CancellationToken ct)
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

    IEnumerable<Task<string>> downloadTasksQuery = from url in URLList select ProcessURL(url, client, ct);

    List<Task<string>> downloadTasks = downloadTasksQuery.ToList();

    while (downloadTasks.Count > 0)
    {
        Task<string> firstFinishedTask = await Task.WhenAny(downloadTasks);

        downloadTasks.Remove(firstFinishedTask);

        string length = await firstFinishedTask;  //this is the data of the first finished download
        int refPoint = length.IndexOf("Download (Save as...): <a href=\"") + 32;
        VideoList.Add(length.Substring(refPoint, length.IndexOf("\">", refPoint) - refPoint));
    }
}

async Task<string> ProcessURL(string url, HttpClient client, CancellationToken ct)
{
    HttpResponseMessage response = await client.GetAsync(url, ct);
    string urlContents = await response.Content.ReadAsStringAsync();
    return urlContents;
}

我确定它的东西相当简单。但我只需要下载的字符串与原始URLList列表的顺序相同。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

未经测试,但这应该有效。

async Task AccessTheWebAsync(CancellationToken ct)
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

    var downloadTasks = new List<Task<Tuple<string, int>>();

    int index = 0;
    foreach(var url in URLList)
        downloadTasks.Add(ProcessURL(url, client, ct, index++));

    while (downloadTasks.Count > 0)
    {
        Task<Tuple<string, int>> firstFinishedTask = await Task.WhenAny(downloadTasks);

        downloadTasks.Remove(firstFinishedTask);

        Tuple<string, int> result = await firstFinishedTask;  //this is the data of the first finished download
        int refPoint = result.Item1.IndexOf("Download (Save as...): <a href=\"") + 32;
        VideoList.Add(length.Item1.Substring(refPoint, length.IndexOf("\">", refPoint) - refPoint));
    }
}

async Task<Tuple<string, int>> ProcessURL(string url, HttpClient client, CancellationToken ct, int index)
{
    HttpResponseMessage response = await client.GetAsync(url, ct);
    string urlContents = await response.Content.ReadAsStringAsync();
    return Tuple.Create(urlContent, index);
}