var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
using (var web = new WebClient())
{
web.DownloadFile(url,save);
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
using (var web = new WebClient())
{
web.DownloadFile(url,save);
}
异步下载
该方法也提供异步的实现:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
using(var web = newWebClient())
{
await web.DownloadFileTaskAsync(url, save);
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, save);
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, save);
}
下载文件的同时向服务器发送自定义请求头
如果需要对文件下载请求进行定制,可以使用 HttpClient :
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
var http = newHttpClient();
var request = newHttpRequestMessage(HttpMethod.Get,url);
//增加 Auth 请求头
request.Headers.Add("Auth","123456");
var response = await http.SendAsync(request);
response.EnsureSuccessStatusCode();
using(var fs = File.Open(save, FileMode.Create))
{
using(var ms = response.Content.ReadAsStream())
{
await ms.CopyToAsync(fs);
}
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
var http = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get,url);
//增加 Auth 请求头
request.Headers.Add("Auth","123456");
var response = await http.SendAsync(request);
response.EnsureSuccessStatusCode();
using (var fs = File.Open(save, FileMode.Create))
{
using (var ms = response.Content.ReadAsStream())
{
await ms.CopyToAsync(fs);
}
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
var http = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get,url);
//增加 Auth 请求头
request.Headers.Add("Auth","123456");
var response = await http.SendAsync(request);
response.EnsureSuccessStatusCode();
using (var fs = File.Open(save, FileMode.Create))
{
using (var ms = response.Content.ReadAsStream())
{
await ms.CopyToAsync(fs);
}
}
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, save);
}
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, save);
}
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
using (var web = new WebClient())
{
try
{
await web.DownloadFileTaskAsync(url, save);
}
catch
{
if (File.Exists(save))
{
File.Delete(save);
}
throw;
}
}
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
using (var web = new WebClient())
{
try
{
await web.DownloadFileTaskAsync(url, save);
}
catch
{
if (File.Exists(save))
{
File.Delete(save);
}
throw;
}
}
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
//先下载到临时文件
var tmp = save + ".tmp";
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, tmp);
}
File.Move(tmp, save, true);
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var url = "https://www.coderbusy.com";
var save = @"D:\1.html";
if (!File.Exists(save))
{
Console.WriteLine("文件不存在,开始下载...");
//先下载到临时文件
var tmp = save + ".tmp";
using (var web = new WebClient())
{
await web.DownloadFileTaskAsync(url, tmp);
}
File.Move(tmp, save, true);
Console.WriteLine("文件下载成功");
}
Console.WriteLine("开始处理文件");
//TODO:对文件进行处理
var downloader = new DownloadService(downloadOpt);
var downloader = new DownloadService(downloadOpt);
配置事件处理器(该步骤可以省略):
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Provide `FileName` and `TotalBytesToReceive` at the start of each downloads
// 在每次下载开始时提供 "文件名 "和 "要接收的总字节数"。
downloader.DownloadStarted += OnDownloadStarted;
// Provide any information about chunker downloads, like progress percentage per chunk, speed, total received bytes and received bytes array to live streaming.
// Provide any information about download progress, like progress percentage of sum of chunks, total speed, average speed, total received bytes and received bytes array to live streaming.
// Provide `FileName` and `TotalBytesToReceive` at the start of each downloads
// 在每次下载开始时提供 "文件名 "和 "要接收的总字节数"。
downloader.DownloadStarted += OnDownloadStarted;
// Provide any information about chunker downloads, like progress percentage per chunk, speed, total received bytes and received bytes array to live streaming.
// 提供有关分块下载的信息,如每个分块的进度百分比、速度、收到的总字节数和收到的字节数组,以实现实时流。
downloader.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
// Provide any information about download progress, like progress percentage of sum of chunks, total speed, average speed, total received bytes and received bytes array to live streaming.
// 提供任何关于下载进度的信息,如进度百分比的块数总和、总速度、平均速度、总接收字节数和接收字节数组的实时流。
downloader.DownloadProgressChanged += OnDownloadProgressChanged;
// Download completed event that can include occurred errors or cancelled or download completed successfully.
// 下载完成的事件,可以包括发生错误或被取消或下载成功。
downloader.DownloadFileCompleted += OnDownloadFileCompleted;
// Provide `FileName` and `TotalBytesToReceive` at the start of each downloads
// 在每次下载开始时提供 "文件名 "和 "要接收的总字节数"。
downloader.DownloadStarted += OnDownloadStarted;
// Provide any information about chunker downloads, like progress percentage per chunk, speed, total received bytes and received bytes array to live streaming.
// 提供有关分块下载的信息,如每个分块的进度百分比、速度、收到的总字节数和收到的字节数组,以实现实时流。
downloader.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
// Provide any information about download progress, like progress percentage of sum of chunks, total speed, average speed, total received bytes and received bytes array to live streaming.
// 提供任何关于下载进度的信息,如进度百分比的块数总和、总速度、平均速度、总接收字节数和接收字节数组的实时流。
downloader.DownloadProgressChanged += OnDownloadProgressChanged;
// Download completed event that can include occurred errors or cancelled or download completed successfully.
// 下载完成的事件,可以包括发生错误或被取消或下载成功。
downloader.DownloadFileCompleted += OnDownloadFileCompleted;
冰爷NB
代码是个好代码,不过能放最终源码方便伸手党复制粘贴也算是功德一件了。