gleeuwdrent
gleeuwdrent

Reputation: 35

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?

Upvotes: 1

Views: 1309

Answers (1)

MartinM43
MartinM43

Reputation: 101

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#

Upvotes: 1

Related Questions