Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to download multiple webpages using C#

This is a (basic) example of what I currently have:

foreach (var uri in uris)
{
    using (var client = new WebClient())
    {
        client.Proxy = null;
        client.DownloadStringCompleted += DownloadComplete;
        client.DownloadStringAsync(uri);
    }
}

Is there a faster way?

like image 983
Lee Crossley Avatar asked Dec 16 '22 09:12

Lee Crossley


2 Answers

The important thing is to make the downloads in parallel, which you are already doing thanks to the Async download.

The download speed of your code is entirely dependent of the actual network transfer speed, so it is as good as it gets.

like image 199
Anders Abel Avatar answered Dec 19 '22 00:12

Anders Abel


I believe you can make it a lot faster if you set Accept-Encoding header to gzip,deflate, if the server support gzip (modern web server should support).

The basic idea is to ask the server zip the content before downloading, normally for a common web page, you may get 50% less in size and hence you can save 50% time.

Look at this: http://csharpfeeds.com/post/5518/HttpWebRequest_and_GZip_Http_Responses.aspx

like image 24
unruledboy Avatar answered Dec 19 '22 00:12

unruledboy