tweetsharp and api streaming?

这一生的挚爱 提交于 2019-12-18 12:43:00

问题


I can't find a recent answer on this.

Tweetsharp reportedly in a number of old posts does not support streaming from the Twitter user stream api. However github shows a class which looks like it does support streaming.

https://github.com/danielcrenna/tweetsharp/blob/cad16546df7c5be6ee528ecfa6171098b662a6ab/src/net40/TweetSharp.Next/Service/TwitterService.Streaming.cs

Does tweetsharp now supports streaming from Twitter, where it did not before

I'm learning c# and I'd appreciate the view of an experienced coder before i continue to spend hours working with tweetsharp.


回答1:


If what you search for is only Twitter's streaming API, you can implement it without a need for an external library

JavaScriptSerializer serializer = new JavaScriptSerializer();

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://stream.twitter.com/1/statuses/sample.json"); 
webRequest.Credentials = new NetworkCredential("...", "...");
webRequest.Timeout = -1;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
while (true)
{
    var line = responseStream.ReadLine();
    if (String.IsNullOrEmpty(line)) continue;

    dynamic obj = serializer.Deserialize<Dictionary<string, object>>(line);

    if(obj["user"]!=null) 
        Console.WriteLine(obj["user"]["screen_name"] + ": " +  obj["text"]);

}


来源:https://stackoverflow.com/questions/9779766/tweetsharp-and-api-streaming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!