1

Forgive me if this is a dumb question, but I'm just trying the SO API, and have run into a basic problem.

If I enter the following URL in a web browser...

https://api.stackexchange.com/2.3/questions?site=stackoverflow.com

...then I get a JSON response as expected.

If I use the exact same URL in a script (in my case using C#, see below), then the result is a regular SO HTML, whose body contains the following (rest of the HTML omitted as I suspect this is all you need)...

<h1 data-translate="block_headline">Sorry, you have been blocked</h1> <h2 class="cf-subheadline"><span data-translate="unable_to_access">You are unable to access</span> api.stackexchange.com</h2> 

I tried adding my API key as a querystring parameter, but it didn't make any difference.

I tried accessing the URL from a private browser window, and got the expected results, so it's not like it picked up on my credentials from another tab.

Anyone able to explain what I'm doing wrong?

Here is the C# code I'm using...

HttpClient client = new(); string uri = "https://api.stackexchange.com/2.3/questions?site=stackoverflow.com"; HttpResponseMessage httpResponse = await client.GetAsync(uri); Console.WriteLine($"Status code: {httpResponse.StatusCode}"); Console.WriteLine($"Response body:\n{await httpResponse.Content.ReadAsStringAsync()}"); 

1 Answer 1

4

The C# HttpClient doesn't add a User-Agent header by default. The CDN provider (Cloudflare in this case) checks for the presence of a User-Agent header to distinguish somewhat between regular browsers and programmatic access.

Just add a User-Agent to your HttpClient and make sure it identifies you.

Identify yourself. Add something useful to the user-agent (ideally, a link to an URL, or something informational) so we can see your bot as something other than "generic unknown anonymous scraper."

See also:

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.