Can anybody tell me how i can download file in my C# program from that URL: http://www.cryptopro.ru/products/cades/plugin/get_2_0
I try to use WebClient.DownloadFile, but i'm getting only html page instead of file.
Can anybody tell me how i can download file in my C# program from that URL: http://www.cryptopro.ru/products/cades/plugin/get_2_0
I try to use WebClient.DownloadFile, but i'm getting only html page instead of file.
Looking in Fiddler the request fails if there is not a legitimate U/A string, so:
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");
I belive this would do the trick.
WebClient wb = new WebClient();
wb.DownloadFile("http://www.cryptopro.ru/products/cades/plugin/get_2_0/cadeplugin.exe","file.exe");
If you need to know the download status or use credentials in order to make the request, I'll suggest this solution:
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");
And her it is the implementation of the callbacks:
void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Download status: {0}%.", e.ProgressPercentage);
}
void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("Download finished!");
}
Try WebClient.DownloadData
You would get response in the form of byte[]
then you can do whatever you want with that.
Sometimes a server would not let you download files with scripts/code. to take care of this you need to set user agent header to fool the server that the request is coming from browser. using the following code, it works. Tested ok
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");
this will work as you expect, and you can download file.
Since this was the first result in my Google search, I thought it would be beneficial to provide an updated answer.
HttpClient
Since WebClient
is deprecated, it's recommended to use HttpClient
for modern C# applications. However, HttpClient
lacks some features like progress reporting and status change events that WebClient
has. Here's an example of how to use HttpClient
to download a file:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string url = "http://www.cryptopro.ru/products/cades/plugin/get_2_0";
string filePath = "c:\\path\\to\\save\\file.exe";
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
await response.Content.CopyToAsync(fileStream);
}
Console.WriteLine("File downloaded successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
DownloadAssistant
LibraryThe DownloadAssistant
is built on top of HttpClient
and provides additional features such as progress reporting, resumable downloads, and more. Here's an example of how to use DownloadAssistant
:
Install the DownloadAssistant
library via NuGet:
Install-Package Shard.DownloadAssistant
Example code using DownloadAssistant
:
using System;
using DownloadAssistant.Requests;
class Program
{
static async Task Main(string[] args)
{
string url = "http://www.cryptopro.ru/products/cades/plugin/get_2_0";
LoadRequestOptions requestOptions = new LoadRequestOptions
{
FileName = "downloadfile",
DestinationPath = @"C:\path\to\save",
RequestCompleated = (req, url) => Console.WriteLine($"Finished successfully: {url}")
};
var request = new LoadRequest(url, requestOptions);
request.Progress.ProgressChanged += (progress, value) => Console.WriteLine($"Download Progress: {value * 100:F2}%");
await request.Task;
}
}
HttpClient
to download a file asynchronously. It handles the response and writes it to a file stream.DownloadAssistant
library, which provides additional features and simplifies the download process.Both methods are valid, but if you need advanced features like progress reporting and resumable downloads, the DownloadAssistant
library is a better choice. Otherwise, HttpClient
is better for basic file downloads.
© 2022 - 2025 — McMap. All rights reserved.