在 Web 请求流式传输时使用 StreamReader 流 .NET?
我正在试验 Twitter 流 api,并尝试打开一个流供用户在事件发生时使用它们。我正在使用一组标准的类来对 twitter 进行 REST api 调用。在中使用 https://userstream.twitter.com/2/user.json 时“GET”调用响应流永远不会结束...我正在打开 StreamReader 并读取响应,就像对 Twitter 进行任何其他 REST 调用一样。这对其他人来说可能是显而易见的,但是我如何“消耗”这个流......有没有办法在 StreamReader 正在读取时读取它(意思是在它关闭之前)?或者也许我可以使用不同的方法来使用这个流......再次,如果这对某些人来说是基本的,我很抱歉,但我目前无法弄清楚......提前感谢您的任何建议或帮助!
这是我开始解决此问题的原始来源...此方法是根据我在 LinkedIn 论坛中找到的一组 C# 类制作的。在读取“responseData = responseReader.ReadToEnd()”的行处,该方法开始“喝”流......但这样做就像无底杯......在关闭之前实时读取此数据流(这是基本上直到我停止调试或终止进程)是我正在解决的问题。
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
更新&相关问题:
所以,我想出了以下方法来保持流打开,并将其写入文件(这不是最终的方法,我只是在测试,开发最好的方法这:)
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
这会将打开的 Twitter 流写入一个文件,每次我发推文、转推文、删除、直接消息...等等...文件都会随着附加到文本的 JSON 对象而增长。我使用了 Do While 0 < 1 在这里进行测试,因为我只是想看看它的工作原理。我在 MSDN StreamReader 构造函数说明 上看到New 构造函数应该接受“leaveOpen”的布尔值,但是当我尝试将其添加到构造函数时不允许这样的参数... 有人有一个很好的例子来说明如何做这与强制和无限循环或只是比这更好的方法...我想简单地阅读每次从 Twitter 发送的新更新,并相应地解决它们? 显然有一种方法,我只是新来的消费这样的流而不关闭它的概念..(**顺便说一句,感谢Dr. Evil的建议,我被引导到这个方向...这并不完全是他的建议,但正是引导我来到这里的原因)
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!!!
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
UPDATE & RELATED QUESTION:
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
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 StreamReader 的缓冲区将接收到的字节提供给 (Encoding.UTF8.GetString),然后将它们附加到 StringBuilder 以接收流,如下所示细绳。
使用
StringBuilder
的ToString
Use
StreamReader
's buffer to feed received bytes to (Encoding.UTF8.GetString
) then append them to aStringBuilder
to get received the stream as string.Use
ToString
ofStringBuilder
预先说明一下:您看到的
leaveOpen
参数仅出现在 .NET 4.5 及更高版本中至于如何处理 Twitter 流 API:
上述链接将引导您访问有关如何访问 Twitter API(两者流 API)的开源库和文档和 REST API)与 .NET / C#。
One remark upfront: The
leaveOpen
parameter you saw is only present in .NET 4.5 and upAs to how to deal with the Twitter streaming API:
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#.