HttpClient not throwing after lost connection

主宰稳场 提交于 2019-12-11 08:08:43

问题


I have a Windows Store application in which I need to download a file a couple of megabytes in size.

I am trying to do this using the HttpClient.

Here is a simplification of the code:

using (var httpClient = new HttpClient())
{
    var request =
    await httpClient.SendAsync(
        new HttpRequestMessage(
        HttpMethod.Get,
        "http://openpandora.info:8080/Battlefield%204%20-%20Fishing%20in%20Baku%20-%20Xbox%20One.mp4"),
        HttpCompletionOption.ResponseHeadersRead);

    var outputFile =
    await
    ApplicationData.Current.LocalFolder.CreateFileAsync(
        "test.data",
        CreationCollisionOption.ReplaceExisting);

    using (var outputStream = await outputFile.OpenStreamForWriteAsync())
    {
        await request.Content.CopyToAsync(outputStream);
    }
}

The sample code provided here is downloading a 2GB file for illustration purposes.

The issue is the following. If the client has no internet connection when the app is started, the code throws an exception as expected. However, if the client loses internet connectivity while the download is running no exception is thrown and the code will never execute beyond the code block. If the client encounters no connection problems during download, the code works fine.

Any insight on why this is the case?

来源:https://stackoverflow.com/questions/20335373/httpclient-not-throwing-after-lost-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!