0

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?

vbNewbie
  • 3,291
  • 15
  • 71
  • 155

1 Answers1

1

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.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • thanks for your response. I abort the webrequest if the loop ends and have now changed it to include a flag which is activated based on a timestamp delay. But my concern is that how can I change this code to keep the connection open and consume all data being sent through this connection. – vbNewbie Apr 03 '12 at 22:07
  • WebRequest's aren't really intended to be persistent unbounded streams. They're intended to make the connection send the request, get the reponse and quit. You're after something more like a NetworkStream. Also, have a look at this question http://stackoverflow.com/questions/3089382/why-do-i-get-to-the-endofstream-in-a-webrequest-if-it-is-a-persistent-keepaliv – JamieSee Apr 03 '12 at 22:45
  • Thank you for your response and that is all I wanted was some good advice. – vbNewbie Apr 04 '12 at 12:22