WebClient图片下载

使用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);
                }
            }

 

posted @ 2018-07-11 14:21  _York  阅读(...)  评论(...编辑  收藏