2010-04-24 43 views
2

我的意思是我可以做這樣的事情:WebClient()可以同時下載多個字符串嗎?

var client = new WebClient(); 

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

    var result2 = client.DownloadString(string("http://example.com/notadd.php")); 
在相同常

像100網址?

回答

2

在.NET 4.0中,最簡單的方法是使用ParallelExtensionsExtras的AsycCache和DownloadStringTask擴展方法。事實上,example for this code涵蓋您的具體情況:

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 
}); 

詳情請參閱here

相關問題