使用 HttpClient 进行异步文件下载

本文关键字:异步 文件下载 HttpClient 使用 | 更新日期: 2023-09-27 18:36:56

我有一个服务,它将csv文件返回到POST请求。我想使用异步技术下载所述文件。虽然我可以获取文件,但我的代码有几个突出的问题和疑问:

1)这真的是异步的吗?

2)有没有办法知道内容的长度,即使它是以分块格式发送的?想想进度条)。

3) 我怎样才能最好地监控进度,以便在所有工作完成之前推迟程序退出。

using System;
using System.IO;
using System.Net.Http;
namespace TestHttpClient2
{
    class Program
    {
        /*
         * Use Yahoo portal to access quotes for stocks - perform asynchronous operations.
         */
        static string baseUrl = "http://real-chart.finance.yahoo.com/";
        static string requestUrlFormat = "/table.csv?s={0}&d=0&e=9&f=2015&g=d&a=4&b=5&c=2000&ignore=.csv";
        static void Main(string[] args)
        {
            while (true) 
            {
                Console.Write("Enter a symbol to research or [ENTER] to exit: ");
                string symbol = Console.ReadLine();
                if (string.IsNullOrEmpty(symbol))
                    break;
                DownloadDataForStockAsync(symbol);
            }
        }
        static async void DownloadDataForStockAsync(string symbol)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(baseUrl);
                    client.Timeout = TimeSpan.FromMinutes(5);
                    string requestUrl = string.Format(requestUrlFormat, symbol);
                    //var content = new KeyValuePair<string, string>[] {
                    //    };
                    //var formUrlEncodedContent = new FormUrlEncodedContent(content);
                    var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
                    var sendTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                    var response = sendTask.Result.EnsureSuccessStatusCode();
                    var httpStream = await response.Content.ReadAsStreamAsync();
                    string OutputDirectory = "StockQuotes";
                    if (!Directory.Exists(OutputDirectory))
                    {
                        Directory.CreateDirectory(OutputDirectory);
                    }
                    DateTime currentDateTime = DateTime.Now;
                    var filePath = Path.Combine(OutputDirectory, string.Format("{1:D4}_{2:D2}_{3:D2}_{4:D2}_{5:D2}_{6:D2}_{7:D3}_{0}.csv",
                        symbol,
                        currentDateTime.Year, currentDateTime.Month, currentDateTime.Day,
                        currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second, currentDateTime.Millisecond
                        ));
                    using (var fileStream = File.Create(filePath))
                    using (var reader = new StreamReader(httpStream))
                    {
                        httpStream.CopyTo(fileStream);
                        fileStream.Flush();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error, try again!");
            }
        }
    }
}

使用 HttpClient 进行异步文件下载

  1. "这真的是异步的吗?"

是的,主要是。DownloadDataForStockAsync() 方法将在操作完成之前返回 await response.Content.ReadAsStreamAsync() 语句。

主要的例外是在方法的末尾附近,您调用 Stream.CopyTo() .这不是异步的,并且由于这是一个可能很长的操作,可能会导致明显的延迟。但是,在控制台程序中,您不会注意到,因为该方法的延续是在线程池中执行的,而不是在原始调用线程中执行的。

如果打算将此代码移动到 GUI 框架(如 Winforms 或 WPF),则应将语句更改为读取await httpStream.CopyToAsync(fileStream);

  1. 有没有办法知道内容的长度,即使它是以分块格式发送的?想想进度条)。

假设服务器在标头中包含Content-Length(并且应该),是的。这应该是可能的。

请注意,如果您使用的是 HttpWebRequest ,则响应对象将具有一个直接为您提供此值的 ContentLength 属性。你在这里使用HttpRequestMessage,我不太熟悉。但据我所知,您应该能够像这样访问Content-Length值:

long? contentLength = response.Content.Headers.ContentLength;
if (contentLength != null)
{
    // use value to initialize "determinate" progress indication
}
else
{
    // no content-length provided; will need to display progress as "indeterminate"
}
  1. 我怎样才能最好地监控进度,以便在所有工作完成之前推迟程序退出。

有很多方法。我会指出,任何合理的方法都需要您更改DownloadDataForStockAsync()方法,以便它返回Task而不是void。否则,您将无权访问创建的任务。无论如何你都应该这样做,所以这没什么大不了的。:)

最简单的方法是只保留您启动的所有任务的列表,然后在退出之前等待它们:

static void Main(string[] args)
{
    List<Task> tasks = new List<Task>();
    while (true) 
    {
        Console.Write("Enter a symbol to research or [ENTER] to exit: ");
        string symbol = Console.ReadLine();
        if (string.IsNullOrEmpty(symbol))
            break;
        tasks.Add(DownloadDataForStockAsync(symbol));
    }
    Task.WaitAll(tasks);
}

当然,这需要您显式维护每个Task对象的列表,包括已完成的对象。如果您打算长时间运行并处理大量符号,则可能会令人望而却步。在这种情况下,您可能更喜欢使用 CountDownEvent 对象:

static void Main(string[] args)
{
    CountDownEvent countDown = new CountDownEvent();
    while (true) 
    {
        Console.Write("Enter a symbol to research or [ENTER] to exit: ");
        string symbol = Console.ReadLine();
        if (string.IsNullOrEmpty(symbol))
            break;
        countDown.AddCount();
        DownloadDataForStockAsync(symbol).ContinueWith(task => countdown.Signal()) ;
    }
    countDown.Wait();
}

这只会递增您创建的每个任务的CountDownEvent计数器,并将延续附加到每个任务以递减计数器。当计数器达到零时,将设置事件,允许返回对Wait()的调用。