c#WebClient DownloadFileAsync()不会引发错误

时间:2018-09-12 15:09:26

标签: c# webclient

我已成功下载以下代码的文件。因为我希望在应用程序启动时下载它们,所以我不想阻止任何内容->异步。

但是我面临的问题是,即使URI路径是完整的废话,它也会生成一个空文件,而不是引发msdn

中所述的错误。

有人可以帮助我吗?

private void DownloadDocuments()
    {
        using (WebClient myWebClient = new WebClient())
        {
            try
            {
                Log("load1");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentActivation), documentspath + "\\" + documentActivation);

            }
            catch (WebException)
            {
                Log("Guide Activation Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Activation could not be saved.");
            }
        }
        using (WebClient myWebClient = new WebClient())
        { 
            try
            {
                Log("load2");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentFloating), documentspath + "\\" + documentFloating);
            }
            catch (WebException)
            {
                Log("Guide Floating Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Floating could not be saved.");
            }
        }
        using (WebClient myWebClient = new WebClient())
        {
            try
            {
                Log("load3");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentSeat), documentspath + "\\" + documentSeat);
            }
            catch (WebException)
            {
                Log("Guide Seat Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Seat could not be saved.");
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

WebClient.DownloadFileAsync 不会在HTTP请求失败时引发异常。您需要预订 DownloadFileCompleted 事件,以获取有关错误的通知。

但是,一旦我们在C#中具有基于任务的异步/等待功能,我不建议您弄乱事件处理程序回调: WebClient.DownloadFileTaskAsync 使用起来更加方便。

考虑到您对并行处理的评论,您可以执行以下操作:

static async Task DownloadDocumentAsync(Uri uri, string fileName)
{
    using (var webClient = new WebClient())
    {
        try
        {
            await webClient.DownloadFileTaskAsync(uri, fileName);
        }
        catch (WebException ex)
        {
            Log($"Downloading {uri} failed. {ex.Message}");
            throw;
        }
        catch (InvalidOperationException)
        {
            Log($"Saving {uri} to {fileName} failed. File is in use.");
            throw;
        }
    }
}

然后在应用程序启动时逻辑:

var baseUri = new Uri(documentsUri);
var downloadTasks = new[]
{
    DownloadDocumentAsync(new Uri(baseUri, documentActivation), Path.Combine(documentspath, documentActivation)),
    DownloadDocumentAsync(new Uri(baseUri, documentFloating), Path.Combine(documentspath, documentFloating)),
    DownloadDocumentAsync(new Uri(baseUri, documentSeat), Path.Combine(documentspath, documentSeat)),
};

try
{
    Task.WaitAll(downloadTasks);
}
catch (AggregateException)
{
    // handle the case when some of the download tasks failed
}

通过这种方式,下载任务可以并行执行,但是 Task.WaitAll 会阻塞,直到所有任务都完成为止。如果要保持异步状态,则需要await Task.WhenAll