我正在从互联网上下载大约1000000个xml文件。
我已经尝试了很多方法,但是当我快速下载时,文件为空。否则会很慢... 我要如何做得更好 最好的问候...
WebClient client = new WebClient();
NetworkCredential myCreds = new NetworkCredential("user", "pass");
client.Credentials = myCreds;
foreach (var id in ids)
{
try
{
string url = "http://....../fileid=" + id;
client.Encoding = Encoding.UTF8;
// client.DownloadFileAsync(new Uri(url), "C:\\Data\\"+id+".xml");
string result= client.DownloadString(url);
WriteFile(id, result);
}
catch (Exception) { }
}
private void WriteFile(decimal id, string text)
{
path ="C:\\Data\\"+id+".xml";
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(text);
}
}