I have this code which I borrowed from this site (https://psycodedeveloper.wordpress.com/2013/04/02/how-to-download-a-file-with-httpclient-in-c/):
public static Task DownloadAsync(string requestUri, string filename)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri");
return DownloadAsync(new Uri(requestUri), filename);
}
public static async Task DownloadAsync(Uri requestUri, string filename)
{
if (filename == null)
throw new ArgumentNullException("filename");
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, 10000, true))
{
await contentStream.CopyToAsync(stream);
}
}
}
}
and I am calling it like this:
DownloadAsync(@"http://www.stafforini.com/docs/Covey%20-%20The%207%20habits%20of%20highly%20effective%20people.pdf", @"D:\Temp\test.pdf").Wait();
but it doesn't download any file and it actually never finish downloading.
What is the problem with this code?
edit1
The answer is deadlock, but I did not know that it has deadlock, so thanks for letting me know that my code has deadlock which I now understand and can fix it.
It is not the duplicate of the other question as I did not know why it was not work, f I knew, that answer would helped me