简体   繁体   English

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

[英]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.

相关问题 如何从响应流中读取数据HttpWebRequest C# - How to read data from response stream HttpWebRequest C# 在c#(。net)system.net.http中,如何将response.statuscode作为int获取?不要使用HttpWebRequest和HttpWebResponse - In c#( .net) system.net.http, how to get the response.statuscode as an int? Not to use HttpWebRequest and HttpWebResponse 使用 http 拦截器在 Angular 应用程序中读取 asp.net 核心 http 响应标头 - Read asp.net core http response header in angular app with http interceptor 使用HttpWebRequest在C#中获取HTTP响应时如何处理JavaScript? - How to deal with JavaScript when fetching http Response in C# using HttpWebRequest? 如何使用httpwebrequest和C#从json api获取数据? - How to get data from json api with c# using httpwebrequest? 如何从HTTP请求读取/输出响应 - How to read/output response from the HTTP request 如何使用ASP.Net/C#读取客户端URL的HTTP标头状态代码 - How to read HTTP-Header status code of client URL by using ASP.Net/C# 在C#中使用RestSharp API读取响应标头 - Read response header using RestSharp API in C# 从调用HttpWebRequest复制HTTP请求/响应头? - Copy HTTP request / response headers from a call to a HttpWebRequest? 如何使用重试后 header 轮询 API 使用 asp.net Z80791B3AE7002CB88F8C2468 客户端 - How to use the retry-after header to poll API using asp.net http client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM