1

Following code:

WebClient client = new WebClient();
client.DownloadProgressChanged += DownloadProgressChanged;
client.DownloadFileCompleted += DownloadFileCompleted;
client.DownloadFileAsync(new Uri(downloadUrl), downloadPath);

works fine in .NET 4.5 - it downloads the file without any issues, however in .NET 3.0 or 3.5 it doesn't work - the file shows 0kb.

Would anybody know why the discrepancy? I am wondering whether there are some additional headers missing from 3.0 that 4.5 has included?

It also doesn't throw any errors.

UPDATE:

Eventually throws "The operation has timed out." exception.

UPDATE #2:

I changed from HTTPS to HTTP and this seems to have worked in my particular case. In actual fact, my HTTP call redirected it to HTTPS. However, by going directly to HTTPS has not worked for me with .NET 3.0.

Sash
  • 1,134
  • 11
  • 23
  • 2
    Well are either of the events fired? If so, what happens? – Jon Skeet May 11 '15 at 13:09
  • Jon, none of the events fire (.NET 3.0). Slight update - I get a The Operation has timed out. I stuck around a little longer for the exception to occur. – Sash May 11 '15 at 13:20
  • If this is HTTP, I suggest you use Wireshark to see what's happening in each case. – Jon Skeet May 11 '15 at 13:23
  • It's an async method so it will exit immediately after it's called. However, this does not mean it's stopped doing what it needs to do to download the file. As with Jon Skeet's question, does Progress Changed/File completed fire? I don't see you handling any potential errors that might occur. It could be the file can't be downloaded again (overwritten) or a timeout has occurred for some reason? – user3036342 May 11 '15 at 13:26
  • Update: throws the above-mentioned exception. It's just weird that if I switch the .NET versions from one to another, it works fine, and vice-versa it doesn't. I was hoping I won't need Wireshark, but I've just downloaded it, so I'll record my findings. – Sash May 11 '15 at 13:28

1 Answers1

0

Default timeout might be different depending on the framework version. This is the case with binding configurations in Web.config files so I suspect it to be the case here as well.

I suggest to create a derived WebClient class and override the GetWebRequest method to set a custom timeout as suggested in this thread: https://stackoverflow.com/a/3052637/2046539 which was inspired by this blog post: http://w3ka.blogspot.be/2009/12/how-to-fix-webclient-timeout-issue.html

As a general rule: never trust default values. You should always use your own values. Most of the time default values are not documented and are an implementation detail.

Community
  • 1
  • 1
Kryptos
  • 875
  • 8
  • 19