开发者

Can WebClient() download more than one string at the same time?

开发者 https://www.devze.com 2022-12-27 20:48 出处:网络
I mean can I do something like this: var client = new WebClient(); var result = client.DownloadString(string(\"http://example.com/add.php\");

I mean can I do something like this:

  var client = new WebClient(); 

  var result = client.DownloadString(string("http://example.com/add.php");

  var result2 = client.DownloadS开发者_运维百科tring(string("http://example.com/notadd.php"));

in paralel like for 100 url's ?


In .NET 4.0, the simplest way is to use the ParallelExtensionsExtras's AsycCache along with the DownloadStringTask extension method. In fact, the example for this code covers your exact scenario:

public sealed class HtmlAsyncCache : AsyncCache<Uri, string>
{
    public HtmlAsyncCache() : 
        base(uri => new WebClient().DownloadStringTask(uri)) { }
}

...

HtmlAsyncCache cache = new HtmlAsyncCache();

var page1 = cache.GetValue(new Uri(“http://msdn.microsoft.com/pfxteam”));
var page2 = cache.GetValue(new Uri(“http://msdn.com/concurrency”));
var page3 = cache.GetValue(new Uri(“http://www.microsoft.com”)); 

Task.Factory.ContinueWhenAll(
    new [] { page1, page2, page3 }, completedPages =>
{
    … // use the downloaded pages here
});

See here for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号