9

My app currently uses OAuth to communicate with the Twitter API. Back in December, Twitter upped the rate limit for OAuth to 350 requests per hour. However, I am not seeing this. I am still getting 150 from the account/rate_limit_status method.

I was told that I needed to use the X-RateLimit-Limit HTTP header to get the new rate limit. However, in my code, I do not see that header.

Here is my code...

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newURL);
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";

using (WebResponse response = request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        responseString = reader.ReadToEnd();
    }
}

If I inspect the response, I can see that it has a property for Headers, and that there are 16 headers. However, I do not have X-RateLimit-Limit in the list.

Image
(source: yfrog.com)

Any idea what I am doing wrong?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ryan Alford
  • 7,514
  • 6
  • 42
  • 56
  • I dont know anything about twitter API, but shouldn't the X-RateLimit-Limit header used in Request, not recieved wia Response? – nothrow Mar 03 '10 at 14:05
  • @Yossarian, no; it should be in the response: http://apiwiki.twitter.com/Rate-limiting – Craig Stuntz Mar 03 '10 at 14:07
  • @Yossarian - the `request` only has two headers: Content-Type and Host. And those are RequestHeaders. The `X-RateLimit-Limit` comes from Twitter, so I would have assumed it would be in the response. – Ryan Alford Mar 03 '10 at 14:10

2 Answers2

12

You should simple be able to use:

using (WebResponse response = request.GetResponse())
{
  string limit = response.Headers["X-RateLimit-Limit"];
  ...
}

If that doesn't work as expected, you can do a watch on response.Headers and see what's in there.

Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • It's working not, if the Response is not an `200 OK`. If the server gives an `550 Internal Server Error` or an `404 Not Found`, the application will break with an `Exception` on `GetResponse`. – Adrian Preuss Jun 12 '18 at 11:35
2

Look at the raw response text (e.g., with Fiddler). If the header isn't there, no amount of C# code is going to make it appear. :) From what you've shown, it seems the header isn't in the response.

Update: When I go to: http://twitter.com/account/rate_limit_status.xml there is no X-RateLimit-Limit header. But when I go to http://twitter.com/statuses/public_timeline.xml, it's there. So I think you just need to use a different method.

It still says 150, though!

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 2
    Thanks Craig. The problem is that the `rate_limit_status` is not rate limited. Neither was the method I was using (the `access_token` OAuth Method). If I use a method that is rate limited, like the `public_timeline`, it's there. So it seems that Twitter only sends those header values for methods that are actually rate limited. – Ryan Alford Mar 03 '10 at 14:32