请问有没有人能告诉我如何在我的C#程序中从以下URL下载文件:
http://www.cryptopro.ru/products/cades/plugin/get_2_0我尝试使用WebClient.DownloadFile,但是我只得到了HTML页面而不是文件。
请问有没有人能告诉我如何在我的C#程序中从以下URL下载文件:
http://www.cryptopro.ru/products/cades/plugin/get_2_0我尝试使用WebClient.DownloadFile,但是我只得到了HTML页面而不是文件。
在 Fiddler 中查看请求,如果没有合法的 U/A 字符串,则请求将失败,因此:
WebClient wb = new WebClient();
wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
wb.DownloadFile("http://www.cryptopro.ru/products/cades/plugin/get_2_0/cadeplugin.exe", "c:\\xxx\\xxx.exe");
WebClient wb = new WebClient();
wb.DownloadFile("http://www.cryptopro.ru/products/cades/plugin/get_2_0/cadeplugin.exe","file.exe");
WebClient client = new WebClient();
Uri ur = new Uri("http://remoteserver.do/images/img.jpg");
client.Credentials = new NetworkCredential("username", "password");
client.DownloadProgressChanged += WebClientDownloadProgressChanged;
client.DownloadDataCompleted += WebClientDownloadCompleted;
client.DownloadFileAsync(ur, @"C:\path\newImage.jpg");
以下是回调函数的实现:
void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Download status: {0}%.", e.ProgressPercentage);
}
void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("Download finished!");
}
WebClient.DownloadData
你将会得到一个byte[]
类型的响应,然后你可以对它进行任何想要的操作。 var webClient=new WebClient();
webClient.Headers["User-Agent"] =
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
webClient.DownloadFile("the url","path to downloaded file");
这将按照您的期望工作,并且您可以下载文件。