Search code examples
c#dotnet-httpclienthttp-status-code-406

Problems downloading file using HttpClient


Im having problems doing something as simple as downloading a file using HttpClient.

When ever execute "GetByteArrayAsync", i get an error stating: "Response status code does not indicate success: 406 (Not Acceptable)."

What am i doing wrong?

class Program
{
    private static readonly HttpClient _httpClient = new HttpClient();

    static async Task Main(string[] args)
    {

        byte[] fileBytes = await _httpClient.GetByteArrayAsync(@"http://url.com/filename.png");

        File.WriteAllBytes(@"Files\filename.png", fileBytes);

    }
}

Solution

  • It seemed like the webserver hosting the files didnt like not having the User-Agent attribute set. After setting it, the request worked.