从Web请求消耗的StreamReader流.NET,因为它流?因为它、消耗、Web、NET

2023-09-03 20:28:38 作者:时间是最好的测谎仪

我尝试用Twitter的流API,并正尝试打开一个流一个用户消费的事件,因为它们发生。我使用的是一套标准的类制作的REST API调用到Twitter。当使用 https://userstream.twitter.com/2/user.json 在GET呼叫响应流永远不会结束......我打开一个StreamReader和阅读的反应,我想同其他REST调用到Twitter。这可能是显而易见的人,但我怎么消费这个流......有没有一种方法来读取StreamReader的,因为它的读取(意味着关闭之前)?或者也许有不同的方法,我可以将用户要使用这个流....一次,我提前道歉,如果这接缝是初级到一些,但我不能算出它的那一刻......感谢您的提醒或帮助!

I'm experimenting with the Twitter streaming api, and am trying to open a stream for a user to consume events as they happen. I'm using a standard set of classes for making REST api calls to twitter. When using https://userstream.twitter.com/2/user.json in a "GET" call the response stream never ends... I'm opening a StreamReader and reading the response as I would with any other REST call to Twitter. This is probably obvious to others, but how do I "consume" this stream... Is there a way to read the StreamReader as it's reading (meaning before it closes)? or maybe there a different method I can user to consume this stream.... again, I apologize if this seams to be elementary to some, but I can't figure it out at the moment... Thanks in advance for any advise or help!!!

下面是我开始......这种方法是从一组C#类,我发现在LinkedIn的论坛制作解决此的原始来源。在读取的行responseData = responseReader.ReadToEnd()方法开始喝流...但这样做就像一个无底杯...读取数据的此流在关闭之前,实时(这是实际上,直到我停止调试或终止的进程)是我解决这个问题。

Here is the original source I started troubleshooting this with... This method was fabricated from a set of C# Classes I found in a forum on LinkedIn. At the line that reads "responseData = responseReader.ReadToEnd()" the method starts to "drink" the stream... but does so like a bottomless cup... reading this stream of data in real time before it closes (which is essentially until I stop debugging or kill the process) is the question I'm tackling.

Private Function WebResponseGet(ByVal webRequest As HttpWebRequest) As String
    Dim responseReader As StreamReader = Nothing
    Dim responseData As String = ""

    Try
        responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
        responseData = responseReader.ReadToEnd()
    Catch
        Throw
    Finally
        webRequest.GetResponse().GetResponseStream().Close()
        responseReader.Close()
        responseReader = Nothing
    End Try

    Return responseData
End Function

更新和放大器;相关的问题:

所以,我想通了,下面的方式来保持流开,并将其写入一个文件(这不会是最后的办法,我只是测试,一个发展中的这样做的最好办法:)

So, I figured out the following way to keep a stream open, and write it to a file (this won't be the final approach, I'm just testing, a developing the best way of doing this :)

Private Sub DrinkIt(ByVal webRequest As HttpWebRequest)

    Dim coder As Encoding = Encoding.UTF8

    Dim stream_reader As New StreamReader(webRequest.GetResponse().GetResponseStream(), coder, True, 1024)

    Do While 0 < 1

        Dim w As IO.StreamWriter

        w = File.AppendText(targetFile)

        Dim c(5) As Char

        stream_reader.Read(c, 0, c.Length)

        w.Write(c)

        w.Close()

        w.Dispose()

    Loop

    stream_reader.Close()

    stream_reader.DiscardBufferedData()

    stream_reader.Dispose()

End Sub

这写我每次鸣叫,转推,删除开通微博流文件,并直接留言......等等......该文件增长与附加到文本的JSON对象。我用的是做,而0℃测试在这里,因为我只是想看看它的工作1。我在 MSDN StreamReader的构造函数描述见新的构造函数应该接受leaveOpen一个布尔值,但没有允许这样的说法,当我试图把它添加到构造... 有没有人对如何使用强迫做一个很好的例子,无限循环或只是一个比这更好的办法......我想简单地阅读新的更新,每次发送的微博,并相应地解决这些问题?,我只是新的概念,显然有一种方法消费是这个样子了它正在关闭的流。(**顺便说一句,由于邪恶博士的建议,我是领导这一方向......这不正是他建议,但就是导致我在这里)

This writes the opened twitter stream to a file, and every time I Tweet, Retweet, Delete, Direct Message... and so on... The file grows with a JSON objects appended to the text. I used the Do While 0 < 1 for testing here, because I just wanted to see it working. I see on MSDN StreamReader Constructor Description that the New constructor is supposed to accept a Boolean value for "leaveOpen", but no such argument allowed when I try to add this to the constructor... Does anyone have a good example of how to do this with forcing and infinite loop or just a better approach than this... I would like to simply read new updates sent each time from Twitter, and address them accordingly? There is obviously a way, I'm just new to the concept of consuming a stream like this with out it being closed.. (**btw, Thanks to Dr. Evil's suggestion, I was lead in this direction... It's not exactly what he suggested, but is what lead me here)

推荐答案

一句话前期:你看到的 leaveOpen 参数仅present研究。 NET 4.5及以上的

One remark upfront: The leaveOpen parameter you saw is only present in .NET 4.5 and up

至于如何应对Twitter的流API:

As to how to deal with the Twitter streaming API:

http://www.voiceoftech.com/swhitley/index.php/2010/04/open-source-net-c-twitter-streaming-api-client/ HTTP://$c$c.google.com/p/twitterstreamclient/ 的http://www.$c$cproject.com/Articles/75003/Creating-a-Simple-Twitter-Client-Application.aspx http://www.voiceoftech.com/swhitley/index.php/2010/04/open-source-net-c-twitter-streaming-api-client/ http://code.google.com/p/twitterstreamclient/ http://www.codeproject.com/Articles/75003/Creating-a-Simple-Twitter-Client-Application.aspx

上面的链接指向你的开源如何访问Twitter的API库和文档(两个的流API和REST API)与.NET / C#。

The above links lead you to opensource libraries and documentation on how to access the Twitter API (both the streaming API and the REST API) with .NET / C#.