c# - 如何使用 .NET HttpWebRequest API 从响应中读取 HTTP header ?

标签 c# twitter oauth httpwebresponse

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

我被告知我需要使用 X-RateLimit-Limit HTTP header 来获得新的速率限制。但是,在我的代码中,我没有看到该 header 。

这是我的代码...

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 个 header 。但是,列表中没有 X-RateLimit-Limit

Image
(来源:yfrog.com)

知道我做错了什么吗?

最佳答案

您应该能够简单地使用:

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

如果这没有按预期工作,您可以观察 response.Headers 并查看其中的内容。

关于c# - 如何使用 .NET HttpWebRequest API 从响应中读取 HTTP header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2371835/

相关文章:

c# - VS 2010 : unable to unit test

c# - “StructureMap.ObjectFactory”已过时

php - Twitter API 在调用 statuses/update.json 时响应 "Your credentials do not allow access to this resource"

python - GET 趋势/地点的问题 (Twython)

ios - 如何使用 post 方法从登录功能将 JSON 数据传递到另一个 View Controller ?

c# - 我应该使用 MethodInvoker 还是 Action

c# - 使用 Github Action 验证 .editorconfig 文件

python - 一分钟后无法停止串流播放

android - 在android中使用路标请求访问 token 时获取401

android - 从 Android 应用程序授权和访问 Google Tasks API?