主要代码如下
for(int i=1;i<urlList.count;i++)
{
Uri uri = new Uri(urlList[i]);
WebClient webClient = new WebClient();
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(AsyncDownloadCompleted);
webClient.DownloadDataAsync(uri);
}
private void AsyncDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
byte[] bytes = e.Result;
string html = Encoding.Default.GetString(bytes);
//到了这里,怎么取得网页的url作为要保存文件的名称??
}
}
4 个解决方案
#1
求高手指点啊~~~初次接触异步~~
#2
urlList.ForEach(x =>
{
var result = await webClient.DownloadDataAsync(new Uri(x));
string html = Encoding.Default.GetString(result);
// x 是你要的url名,不过保存的时候要记得过滤诸如 / ? 等路径中不允许的值。
});
你可以看到,几行代码代替了你原先两个方法的十多条代码。
顺便说说C# 5之前你可以怎么做:
for(int i=1;i<urlList.count;i++)
{
Uri uri = new Uri(urlList[i]);
WebClient webClient = new WebClient();
webClient.BaseAddress = urlList[i];
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(AsyncDownloadCompleted);
webClient.DownloadDataAsync(uri);
}
private void AsyncDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
byte[] bytes = e.Result;
string html = Encoding.Default.GetString(bytes);
var wc = sender as WebClient;
//到了这里,怎么取得网页的url作为要保存文件的名称??
// yoururl = wc.BaseAddress;
}
}
#3
public class CustWebClient
{
private WebClient webclient;
public string Url;
public CustWebClient(string u)
{
Url = u;
webclient = new WebClient();
webclient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webclient_DownloadDataCompleted);
webclient.DownloadDataAsync(new Uri(Url));
}
void webclient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
//这里直接使用URL作为路径就可以了
}
}
不知道这样是否可以。
#4
for循环可以直接遍历CustWebClient这个类就可以了。
#1
求高手指点啊~~~初次接触异步~~
#2
urlList.ForEach(x =>
{
var result = await webClient.DownloadDataAsync(new Uri(x));
string html = Encoding.Default.GetString(result);
// x 是你要的url名,不过保存的时候要记得过滤诸如 / ? 等路径中不允许的值。
});
你可以看到,几行代码代替了你原先两个方法的十多条代码。
顺便说说C# 5之前你可以怎么做:
for(int i=1;i<urlList.count;i++)
{
Uri uri = new Uri(urlList[i]);
WebClient webClient = new WebClient();
webClient.BaseAddress = urlList[i];
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(AsyncDownloadCompleted);
webClient.DownloadDataAsync(uri);
}
private void AsyncDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
byte[] bytes = e.Result;
string html = Encoding.Default.GetString(bytes);
var wc = sender as WebClient;
//到了这里,怎么取得网页的url作为要保存文件的名称??
// yoururl = wc.BaseAddress;
}
}
#3
public class CustWebClient
{
private WebClient webclient;
public string Url;
public CustWebClient(string u)
{
Url = u;
webclient = new WebClient();
webclient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webclient_DownloadDataCompleted);
webclient.DownloadDataAsync(new Uri(Url));
}
void webclient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
//这里直接使用URL作为路径就可以了
}
}
不知道这样是否可以。
#4
for循环可以直接遍历CustWebClient这个类就可以了。