1

I would like to convert several data sets with a service in the background through Nominatim server. Now I've invested a lot of time to get an answer from the nominatim with the Http client class. Unfortunately, I only ever get a 404 error

Here is my sample code:

namespace ConsoleApp7
{
internal class Program
{
    static void Main(string[] args)
    {
        Task t = new Task(DownloadPageAsync);
        t.Start();
        Console.WriteLine("Downloading page...");
        Console.ReadLine();
    }

    static async void DownloadPageAsync()
    {
        // ... Target page.
        string page = "https://nominatim.openstreetmap.org/reverse? 
        format=geocodejson&lat=60.2299&lon=11.1663";

        // ... Use HttpClient.
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await 
        client.GetAsync(page))
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            string result = await content.ReadAsStringAsync();

            // ... Display the result.
            if (result != null)
            {
                Console.WriteLine(result);
            }
        }
    }
}

}

I'm happy about any help!

Many thanks

Markus
  • 20,838
  • 4
  • 31
  • 55
  • Perhaps you need to [set the User-Agent header](https://stackoverflow.com/questions/44076962/how-do-i-set-a-default-user-agent-on-an-httpclient)? This works for me from Postman so that seems likely to be the issue. – ProgrammingLlama Aug 10 '22 at 05:58
  • It seems the [usage policy](https://operations.osmfoundation.org/policies/nominatim/) states: _"Provide a valid HTTP Referer or User-Agent identifying the application (stock User-Agents as set by http libraries will not do)."_, which lends credence to my theory. – ProgrammingLlama Aug 10 '22 at 06:04
  • According to @DiplomacyNotWar response, try adding the User-Agent header with the following line client.DefaultRequestHeaders.Add("User-Agent", ".NET Application"); – 4D1C70 Aug 10 '22 at 06:06

2 Answers2

3

The problem is that website doesn't allow any default http request without user agent. You can solve this adding a useragent to your request. Code like below:

static void Main(string[] args)
{
    Task t = new Task(DownloadPageAsync);
    t.Start();
    Console.WriteLine("Downloading page...");
    Console.ReadLine();
}

static async void DownloadPageAsync()
{
    // ... Target page.
    string page = "https://nominatim.openstreetmap.org/reverse?format=geocodejson&lat=60.2299&lon=11.1663";

    // ... Use HttpClient.
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/2.0)");

        using (HttpResponseMessage response = await
    client.GetAsync(page))
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            string result = await content.ReadAsStringAsync();

            // ... Display the result.
            if (result != null)
            {
                Console.WriteLine(result);
            }
        }
    }
}
Ramin Quliyev
  • 382
  • 2
  • 8
1

You simply need to set a User-Agent

var page = "https://nominatim.openstreetmap.org/reverse?format=geocodejson&lat=60.2299&lon=11.1663";

// ... Use HttpClient.
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "WHATEVER VALUE");
using var response = await client.GetAsync(page);
using var content = response.Content;
// ... Read the string.
var result = await content.ReadAsStringAsync();

// ... Display the result.
if (result != null) Console.WriteLine(result);
Daniel
  • 9,491
  • 12
  • 50
  • 66