如何使用.NET HttpWebRequest API从响应中读取HTTP头?

时间:2010-03-03 13:59:29

标签: c# twitter oauth httpwebresponse

我的应用目前使用OAuth与Twitter API进行通信。早在去年12月,Twitter就将OAuth的费率上限提高到每小时350个请求。但是,我没有看到这一点。我仍然从account/rate_limit_status方法得到150。

有人告诉我,我需要使用X-RateLimit-Limit HTTP标头来获取新的速率限制。但是,在我的代码中,我没有看到那个标题。

这是我的代码......

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();
    }
}

如果我检查response,我可以看到它有Headers的属性,并且有16个标题。但是,我在列表中没有X-RateLimit-Limit

Image http://img10.yfrog.com/img10/5997/33201085434am.png

知道我做错了吗?

2 个答案:

答案 0 :(得分:12)

你应该能够简单地使用:

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

如果这不能按预期工作,你可以看一下response.Headers,看看那里有什么。

答案 1 :(得分:2)

查看原始回复文本(例如,使用Fiddler)。如果没有标题,则不会显示任何数量的C#代码。 :)从您所显示的内容来看,标题似乎不在回复中。

更新:当我转到:http://twitter.com/account/rate_limit_status.xml时,没有X-RateLimit-Limit标头。但是当我去http://twitter.com/statuses/public_timeline.xml时,它就在那里。所以我认为你只需要使用不同的方法。

尽管如此,它仍然说是150!