Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have gotten a weird issue. The code I am using was actually used before and it actually worked. Now, I am unsure what has happened, but now the code is not working. It will place a file and will not add its content at all. The url/uri is correct, because I checked by using breakpoints and manually entering that url in a webbrowser. I am using .NET 5.0.

What I have tried:

C#
public void BeginDownload(string url, string destination)
{
    Uri uri = new Uri(url);
    string filename = url.Substring(url.LastIndexOf("/") + 1);
    webClient = new WebClient();
    webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
    webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
    String newFile = destination + filename;
    webClient.DownloadFileAsync(uri, @newFile);
}
Posted
Updated 14-Feb-22 13:39pm

Your code would fail if destination doesn't end on a backslash.
Rather than string concatenations, you'd better use appropriate methods to parse or build paths. such as Path.GetFileName() and Path.Combine().

:)
I
 
Share this answer
 
v2
Comments
CoderzF1 13-Feb-22 20:21pm    
seems weird that there is an empty file actually placed in the destination directory. there shouldnt even be a file present at all if the destination is incorrect
Luc Pattyn 13-Feb-22 20:50pm    
true. Is the empty file timestamped according to your latest try run?

You did not show the code for Progress and Completed handlers.
If anything goes wrong there, the download might come to a standstill right away.
Luc Pattyn 13-Feb-22 21:14pm    
I was able to replicate.
In my case the download did never take off due to a security issue.
This can be seen in the e.Error field at the Completed handler.
It is all about TLS.
What is needed is executing once a statement like this:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

the latter works on older .NET versions that don't know about Tls12.

I strongly suggest to always provide code that checks the e.Error field in competion handlers (same for BackgroundWorker and many others).

:)
CoderzF1 14-Feb-22 18:40pm    
I have tried this security protocol. I have also checked the error message. All it gives is [The remote server returned an error: (500) Internal Server Error.]
When i try to download the file, it adds a file to the directory and it has a time stamp, but it also shows 0 bytes for the file size.
Luc Pattyn 14-Feb-22 18:57pm    
From my observation downloading a file from a secured site (https://... URL): creating an empty file is the first thing it does. When that succeeds it starts trying to contact the site and start the download. Which failed without the TLS12 option, and succeeded with it. The failure was a WebException "request aborted. Could not create SSL/TLS secure channel" which is much more specific than a vague "500. Internal error"

I used .NET FrameWork 4.5 but I can't imagine that matters...

Can you download the file manually i.e. with a browser?
if so, does that need login in?
if so, your code needs to provide credentials

Does the problem also exist with other URLs? Can you show a failing URL?
I managed to figure out what I needed to do.
I figured out that I needed to set the user agent for the webclient apparently. Still seems weird that the code worked fine 2 months ago. Oh Well. I would like to thank Luc Pattyn for attempting to help...5 stars.

This is the code that actually worked.
C#
public void BeginDownload(string url, string destination)
{
    Uri uri = new Uri(url);
    string filename = url.Substring(url.LastIndexOf("/") + 1);
    webClient = new WebClient();
    webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +
"Windows NT 5.2; .NET CLR 1.0.3705;)");
    webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
    webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
    String newFile = destination + filename;
    webClient.DownloadFileAsync(uri, @newFile);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900