3

I am currently playing with the gravatar api, by writing a C# application.

Firstly, I tried to get my gravatar picture. It worked.

After that, I tried to get the profile QR code. It worked.

Now, I want retrieve my profile information in json. I followed the indications given by the website, and I wrote this:

using (var web = new HttpClient())
{
    using (var response = await web.GetAsync("http://www.gravatar.com/205e460b479e2e5b48aec07710c08d50"))
    {
        // ...

Every time I try that, I get an error 403. But when I copy-paste the url in my web browser, or if I do a wget, it works fine an returns the expected result.

I also tried with this, but I get the error too:

var request = WebRequest.Create("http://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.json");
using (var response = request.GetResponse())
{
    // ...

Is somebody has an idea of what is wrong in my way to do?

paulinodjm
  • 205
  • 1
  • 2
  • 6

1 Answers1

3

In this case, it doesn't like that you're not passing the User-Agent HTTP header (and I don't think you can as a WebRequest, so we cast it to HttpWebRequest):

var request = (HttpWebRequest)WebRequest.Create("http://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.json");
        request.UserAgent = "Whatever user agent you'd like to use here...";
using (var response = request.GetResponse())
{
// ...

EDIT: It may help you in the future to use the likes of Fiddler, so that you may attempt to reproduce your browser's behaviour.

Dasanko
  • 579
  • 8
  • 16
  • You don't have to cast it to `HttpWebRequest`, you can add it to `WebRequest.Headers` manually. – rocky Jan 25 '17 at 14:58