1

I have 2 urls:

https://static.summitracing.com/global/images/prod/xlarge/cse-5160_xl.jpg http://www.americanbassusa.com/Subwoofers/DX-Series-Images/DX/DX-front.jpg

Both download the corresponding images correctly when opened in Chrome but when using the following console application to download them using WebClient.DownloadData(String), the second url causes a System.Net.WebException to be thrown.

Can any one provide me with an understanding as to why this is happening?

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();
    }
}

The exception details are:

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
Dangerous
  • 4,818
  • 3
  • 33
  • 48
  • 2
    Please paste the exception details into your question, A `WebException` can be anything – Patrick Hollweck Apr 19 '19 at 18:03
  • 1
    What's the actual error message inside the exception though? – Caius Jard Apr 19 '19 at 18:03
  • Apologies, I have added the exception message to the question. – Dangerous Apr 19 '19 at 18:06
  • 1
    A 400 just means that the server software thought your request was invalid. It's up to the server software to determine that, so each site is different. Maybe your request is missing headers or has a bad user-agent string, maybe it's something else, who knows? – gunr2171 Apr 19 '19 at 18:07
  • You might want to use [`DownloadFile`](https://stackoverflow.com/questions/24797485/how-to-download-image-from-url) rather than `DownloadData`. – gunr2171 Apr 19 '19 at 18:10
  • Thanks Caius Jard. Thats a good tip. I'll try that. – Dangerous Apr 19 '19 at 18:17

1 Answers1

3

This particular server wants a useragent string that indicates a browser is in use. This works:

    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");
        }
    }

No particular reason I used downloadfile- just playing around with stuff. If you need the byte array in your app then surely use downloaddata :)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80