-5

I have the following routine that downloads a file from a web server. Sometimes the local file is created, but is zero bytes. I am [almost] certain the remote resource exists.

private static void DownloadFile(string strUrl, string strLocalFile)
{
    using (var client = new HttpClient())
    {
        using (var httpClient = new HttpClient())
        {
            using (var streamAsync = httpClient.GetStreamAsync(strUrl))
            {
                using (var fileStream = new FileStream(strLocalFile, FileMode.OpenOrCreate))
                {
                    streamAsync.Result.CopyTo(fileStream);
                }
            }
        }
    }
}
4
  • 7
    I don't know this library, but perhaps ...Async without await is the issue. Commented yesterday
  • 10
    why do you have two HttpClients ? Commented yesterday
  • How exactly do you observe zero bytes? Probably, in the general case, you do it before the task execution is complete. Alternatively to await, you can generally handle Task.WhenAll, Task.WhenAny, or Task.ContinueWith, and check the result in the handler. Commented yesterday
  • @GeneralGrievance Result blocks until the Task<> is complete. This isn't the recommended way to consume an async API, but it should work. Commented yesterday

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.