tweetsharp and api streaming?

2020-02-26 02:21发布

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.

标签: c# tweetsharp
1条回答
时光不老,我们不散
2楼-- · 2020-02-26 03:02

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"]);

}
查看更多
登录 后发表回答