I have created a httpwebrequest connection to a streaming API after trying a tTCPClient which just never ended up working. My concern is whether my code is correct and that I am actually reading in new data and that the connection is maintained. Initially I had been reading into a buffer and just loaded everything into a file after max size, but figured it would be simpler to read a line, since each entry was being sent delimited by line feeds.

rStream = webrequest.GetResponse().GetResponseStream
rStream = New GZipStream(rStream, CompressionMode.Decompress)
If rStream.CanRead then
   Dim bufferPit(8100) as byte
   Do
      Dim dStream as StreamReader = New StreamReader(rStream)
      While not dStream.EndOfStream
          rData = dStream.ReadLine()
          pTools.appendToFile(rData)
      End While
   .....//some other exception handling
   Loop While rStream.CanRead

It looks like I am continuously reading and not sure if I am reading redundant data here. Also another question is that if I were to use a thread to appendToFile, would that maintain the connection to the stream?

有帮助吗?

解决方案

You're misusing CanRead. Best have a look at the documentation again. CanRead only tells you if a stream is CAPABLE of being read, not if it has data and so should never be used in a loop condition.

Also, you need to Close the stream when you finish with it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top