[英]How to read HTTP header from response using .NET HttpWebRequest API?
My app currently uses OAuth to communicate with the Twitter API. 我的应用目前使用OAuth与Twitter API进行通信。 Back in December, Twitter upped the rate limit for OAuth to 350 requests per hour. 早在去年12月,Twitter就将OAuth的费率上限提高到每小时350个请求。 However, I am not seeing this. 但是,我没有看到这一点。 I am still getting 150 from the account/rate_limit_status method. 我仍然从account / rate_limit_status方法获得150。
I was told that I needed to use the X-RateLimit-Limit
HTTP header to get the new rate limit. 有人告诉我,我需要使用X-RateLimit-Limit
HTTP标头来获得新的速率限制。 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. 如果我检查response
,我可以看到它有Headers
的属性,并且有16个头。 However, I do not have X-RateLimit-Limit
in the list. 但是,我没有列表中的X-RateLimit-Limit
。
(source: yfrog.com ) (来源: yfrog.com )
Any idea what I am doing wrong? 知道我做错了什么吗?
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. 如果这不能按预期工作,你可以看一下response.Headers并看看那里有什么。
Look at the raw response text (eg, with Fiddler). 查看原始响应文本(例如,使用Fiddler)。 If the header isn't there, no amount of C# code is going to make it appear. 如果没有标题,则不会显示任何数量的C#代码。 :) 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. 更新:当我访问: http : //twitter.com/account/rate_limit_status.xml时 ,没有X-RateLimit-Limit
标头。 But when I go to http://twitter.com/statuses/public_timeline.xml , it's there. 但是当我访问http://twitter.com/statuses/public_timeline.xml时 ,它就在那里。 So I think you just need to use a different method. 所以我认为你只需要使用不同的方法。
It still says 150, though! 但它仍然说150!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.