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},已完成页面=>
{
…//在此处使用下载的页面
});
有关更多详细信息,请参阅