Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebClient()能否同时下载多个字符串?_C#_.net_Wpf_Webclient - Fatal编程技术网

C# WebClient()能否同时下载多个字符串?

C# WebClient()能否同时下载多个字符串?,c#,.net,wpf,webclient,C#,.net,Wpf,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个url 在.NET4.0中,最简单的方法是使用的Asycache

我的意思是我能做这样的事情吗:

  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个url

在.NET4.0中,最简单的方法是使用的Asycache和DownloadStringTask扩展方法。事实上,以下内容涵盖了您的具体场景:

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
});
公共密封类HtmlAsyncCache:AsyncCache
{
公共HtmlAsyncCache():
基本(uri=>new WebClient().DownloadStringTask(uri)){}
}
...
HtmlAsyncCache缓存=新的HtmlAsyncCache();
var page1=cache.GetValue(新Uri(“http://msdn.microsoft.com/pfxteam”));
var page2=cache.GetValue(新Uri(“http://msdn.com/concurrency”));
var page3=cache.GetValue(新Uri(“http://www.microsoft.com”)); 
Task.Factory.continuewhalll(
新建[]{page1,page2,page3},已完成页面=>
{
…//在此处使用下载的页面
});
有关更多详细信息,请参阅