Search code examples
c#jsonhttp-status-code-404webclient

Why does the WebClient.DownloadFile method not work and say "JSON not found" while the URL is working using a Web browser?


I am trying to download a file with C# using this code:

using (var client = new WebClient())
{
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    string downloadUrl = "https://data.3dbag.nl/cityjson/v21031_7425c21b/3dbag_v21031_7425c21b_8044.json";
    string destinationFile = "test.json";
    client.DownloadFile(downloadUrl, destinationFile);
}

An example url is this: https://data.3dbag.nl/cityjson/v21031_7425c21b/3dbag_v21031_7425c21b_8044.json

This URL is working in my browser, but in C# I'm getting an (404) Not Found error. Does anyone know how to fix this?


Solution

  • It looks like this server requires the header Accept-Encoding: gzip:

    using (var client = new WebClient())
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        string downloadUrl = "https://data.3dbag.nl/cityjson/v21031_7425c21b/3dbag_v21031_7425c21b_8044.json";
        string destinationFile = "test.json";
        client.Headers.Add("Accept-Encoding", "gzip"); //<-- add Header!
        client.DownloadFile(downloadUrl, destinationFile);
    }
    

    You will get an "compressed response".

    So you will have to decompress the response!

    Compression/Decompression string with C#