为什么在对 url 使用 WebClient.DownloadData 时出现异常?
Why do I get an exception when using WebClient.DownloadData for a url?
我有 2 个网址:
https://static.summitracing.com/global/images/prod/xlarge/cse-5160_xl.jpg
http://www.americanbassusa.com/Subwoofers/DX-Series-Images/DX/DX-front.jpg
两者都在 Chrome 中打开时正确下载相应的图像,但是当使用以下控制台应用程序使用 WebClient.DownloadData(String), the second url causes a System.Net.WebException 下载它们时会被抛出。
谁能告诉我为什么会这样?
class Program
{
static void Main(string[] args)
{
const string WorkingUrl = "https://static.summitracing.com/global/images/prod/xlarge/cse-5160_xl.jpg";
const string NonWorkingUrl = "http://www.americanbassusa.com/Subwoofers/DX-Series-Images/DX/DX-front.jpg";
try
{
using (var client = new WebClient())
{
byte[] fileData = client.DownloadData(NonWorkingUrl);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
异常详情为:
System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadData(Uri address)
at System.Net.WebClient.DownloadData(String address)
at ConsoleApp4.Program.Main(String[] args) in C:\Users\davidandrewpowell\source\repos\ConsoleApp4\ConsoleApp4\Program.cs:line 17
这个特定的服务器需要一个用户代理字符串来指示浏览器正在使用中。这有效:
using (var client = new System.Net.WebClient())
{
client.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134");
client.DownloadFile(NonWorkingUrl, "c:\temp\a");
}
}
我使用 downloadfile 没有特别的原因 - 只是玩弄一些东西。如果您的应用程序中需要字节数组,那么一定要使用 downloaddata :)
我有 2 个网址:
https://static.summitracing.com/global/images/prod/xlarge/cse-5160_xl.jpg http://www.americanbassusa.com/Subwoofers/DX-Series-Images/DX/DX-front.jpg
两者都在 Chrome 中打开时正确下载相应的图像,但是当使用以下控制台应用程序使用 WebClient.DownloadData(String), the second url causes a System.Net.WebException 下载它们时会被抛出。
谁能告诉我为什么会这样?
class Program
{
static void Main(string[] args)
{
const string WorkingUrl = "https://static.summitracing.com/global/images/prod/xlarge/cse-5160_xl.jpg";
const string NonWorkingUrl = "http://www.americanbassusa.com/Subwoofers/DX-Series-Images/DX/DX-front.jpg";
try
{
using (var client = new WebClient())
{
byte[] fileData = client.DownloadData(NonWorkingUrl);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
异常详情为:
System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadData(Uri address)
at System.Net.WebClient.DownloadData(String address)
at ConsoleApp4.Program.Main(String[] args) in C:\Users\davidandrewpowell\source\repos\ConsoleApp4\ConsoleApp4\Program.cs:line 17
这个特定的服务器需要一个用户代理字符串来指示浏览器正在使用中。这有效:
using (var client = new System.Net.WebClient())
{
client.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134");
client.DownloadFile(NonWorkingUrl, "c:\temp\a");
}
}
我使用 downloadfile 没有特别的原因 - 只是玩弄一些东西。如果您的应用程序中需要字节数组,那么一定要使用 downloaddata :)