stream connection using httpwebrequest

Go To StackoverFlow.com

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?

2012-04-03 19:47
by vbNewbie
"Something does not seem right" is not a description of a problem - Kiril 2012-04-03 20:27
does the code above look like it accomplishes a continous connection to a strea - vbNewbie 2012-04-03 21:06
does the output look like it does?? You are the one running your code.. are you getting what you expect - Sam Axe 2012-04-03 22:29


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.

2012-04-03 21:24
by JamieSee
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 2012-04-03 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-keepali - JamieSee 2012-04-03 22:45
Thank you for your response and that is all I wanted was some good advice - vbNewbie 2012-04-04 12:22
Ads