使用WebClient下载文件非常方便,针对有部分网站通过请求头的Referer,做了图片防盗链,可以在webClient加上Referer 来模拟请求
string basePath = Path.Combine(Environment.CurrentDirectory, "imgs");
if (!System.IO.Directory.Exists(basePath))
{
System.IO.Directory.CreateDirectory(basePath);
}
WebClient webClient = new WebClient();
foreach (var src in imgSrcList)
{
try
{
webClient.Headers.Add("Referer", "原始网址");
webClient.DownloadFile(src, Path.Combine(basePath, Path.GetFileName(src)));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
更多专业前端知识,请上 【猿2048】www.mk2048.com