我想在我的WPF应用程序中使用推特流API,我能够使用OAuth完成身份验证过程。我的问题是,我不知道从twitter获取推文的过程。谁能提供一些示例应用程序,可以帮助我理解获取和更新推文的过程。
谢谢。
发布于 2011-11-15 00:32:45
从Twitter流中读取内容非常简单。您只需发出请求,获取响应流,然后开始读取。例如:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(twitter_search_url);
// this uses password authentication. Change for OAuth
request.Credentials = new NetworkCredential(TwitterUser, TwitterPassword);
request.KeepAlive = true;
request.UserAgent = user_agent_string;
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
string line;
while ((line = reader.ReadLine()) != null)
{
// line contains the tweet.
// parse with a JSON reader
}
// end of stream
}
catch (... handle exceptions here)
{
}
finally
{
response.Close();
}响应流本质上是无限的,因此您将永远停留在ReadLine循环中。
几年来,我一直在使用上面代码的一个稍微完整的版本,没有遇到任何问题。当然,生产代码有更好的错误处理,循环会检查Shutdown事件,这样我就可以干净利落地退出它,但读取Twitter流的机制是相同的。
https://stackoverflow.com/questions/8122233
复制相似问题