Multi Threaded File Downloader Using Proxy c#
My开发者_如何学编程 goal is to create a class that gets a link and download it to local drive. The program should support downloading from http\https\ftp links and moreover in my work i have a dedicated proxy(with no authentication) for it. the main system is downloader over asp.net which each student can request a file to download. all the files are pretty big files from 50MB to unlimited size so it should be a fast downloader.
this is my currentcode:
public void DownloadLink(string link)
{
string filename ;
try
{
WebClient wc = new WebClient();
Uri downloadLink= new Uri(link);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
filename = link.Substring(link.LastIndexOf("/") + 1);
Console.WriteLine("Downloading File" + filename);
wc.DownloadFileAsync(downloadLink, filename);
}
catch (Exception i)
{
System.Console.WriteLine ("Failed" + i.ToString());
}
}
private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Download Completed");
}
here are my questions: 1.How can implement simultaneous downloads? 2.how to set the Webclient with my proxy? 3 Does the WebClient.DownloadAsync supports https&ftp or should i handle it? 4. Is there a way to implement the download to be faster? 5.I saw few implementations that included HttpWebRequest,HttpWebResponse & Streams. does it has better preformance?
1 How can implement simultaneous downloads?
You could spawn a task for each download - have a look at the Task Parallel Library (TPL). Make sure that you don't over saturate your internet connection. Spawning too many download threads actually can have a detrimental effect on throughput.
2 how to set the Webclient with my proxy?
You have to set the Proxy property.
3 Does the WebClient.DownloadAsync supports https&ftp or should i handle it?
This shouldn't have to do anything with Async or not -
Ftp: Yes, there are examples here
Https: Yes, there's a clarification here
4 Is there a way to implement the download to be faster?
I doubt that using WebRequest
/ streams directly will gain you anything, most of the time cost comes from actually transferring the data to the local machine.
精彩评论