WebClient DownloadFileAsync() 不起作用
WebClient DownloadFileAsync() does not work
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据...
有线索吗?
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It works fine.
client.DownloadFile(urlAddress, @"D:.xlsx");
}
/*using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It y creats file with 0 bytes. Dunow why is it.
client.DownloadFileAsync(new Uri(urlAddress), @"D:.xlsx");
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}*/
}
catch (Exception ex)
{
}
}
您需要在异步下载完成时保留程序 运行,因为它在另一个线程中运行。
尝试这样的操作,等待它说完成,然后按回车键结束程序:
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
client.DownloadFileAsync(new Uri(urlAddress), @"D:.xlsx");
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}
catch (Exception ex)
{
}
Console.ReadLine();
}
public static void Completed(object o, AsyncCompletedEventArgs args)
{
Console.WriteLine("Completed");
}
根据您使用的应用程序类型,主线程需要在后台线程下载文件时保持 运行。
通过将 Main 函数声明为 async
,您还可以将 DownloadFileTaskAsync
与 await
一起使用。
public static async void Main(string[] args)
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
var fileName = @"D:.xlsx";
using (var client = new WebClient())
{
await client.DownloadFileTaskAsync(new Uri(urlAddress), fileName);
}
}
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据...
有线索吗?
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It works fine.
client.DownloadFile(urlAddress, @"D:.xlsx");
}
/*using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
// It y creats file with 0 bytes. Dunow why is it.
client.DownloadFileAsync(new Uri(urlAddress), @"D:.xlsx");
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}*/
}
catch (Exception ex)
{
}
}
您需要在异步下载完成时保留程序 运行,因为它在另一个线程中运行。
尝试这样的操作,等待它说完成,然后按回车键结束程序:
static void Main(string[] args)
{
try
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential("UserName", "Password");
client.DownloadFileAsync(new Uri(urlAddress), @"D:.xlsx");
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
}
catch (Exception ex)
{
}
Console.ReadLine();
}
public static void Completed(object o, AsyncCompletedEventArgs args)
{
Console.WriteLine("Completed");
}
根据您使用的应用程序类型,主线程需要在后台线程下载文件时保持 运行。
通过将 Main 函数声明为 async
,您还可以将 DownloadFileTaskAsync
与 await
一起使用。
public static async void Main(string[] args)
{
var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
var fileName = @"D:.xlsx";
using (var client = new WebClient())
{
await client.DownloadFileTaskAsync(new Uri(urlAddress), fileName);
}
}