HttpListenerException通过Webclient从C#控制台应用程序从网站下载图像时出错
我想通过C#控制台应用程序中的webclient下载图像,但它在Httplistener中抛出错误,无法下载图像。我甚至用网络浏览器下载,但失败了。如有任何建议,将不胜感激HttpListenerException通过Webclient从C#控制台应用程序从网站下载图像时出错,c#,webclient-download,C#,Webclient Download,我想通过C#控制台应用程序中的webclient下载图像,但它在Httplistener中抛出错误,无法下载图像。我甚至用网络浏览器下载,但失败了。如有任何建议,将不胜感激 try { using (WebClient fileReader = new WebClient()) { fileReader.Headers.Add("User-Agent", "Mozilla/5.0 (Window
try
{
using (WebClient fileReader = new WebClient())
{
fileReader.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:25.0) Gecko/20100101 Firefox/25.0");
fileReader.Headers.Add("Accept-Language", "en-US,en;q=0.5");
fileReader.Headers.Add("Accept-Encoding", "gzip,deflate");
fileReader.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
strFilename = strSaveAs.ToString().Trim() + ".jpg";
if (System.IO.File.Exists(returnImg) == false)
{
fileReader.DownloadFile(strURI, returnImg);
Console.WriteLine("Finished downloading image " + strFilename.ToString());
}
}
}
catch (HttpListenerException ex)
{
Console.WriteLine("Error accessing " + strURI + " - " + ex.Message, true);
//throw;
returnImg = null;
}
catch (Exception ex)
{
Console.WriteLine("Error accessing " + strURI + " - " + ex.Message, true);
//throw;
returnImg = null;
}
错误是什么?为什么失败?@ChrisLava,我遇到了这个错误:在WebClient请求期间发生了异常。