0

Say I create an HttpWebRequest and call it's GetResponseStream method. When I attempt to read the stream has all of the data already been copied to a local buffer, or does it work by reading it as it comes across the wire?

btlog
  • 4,760
  • 2
  • 29
  • 38
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191

2 Answers2

2

The GetResponseStream method returns a specific implementation of the Stream abstract class which is a NetworkStream. This type of stream is bound to a socket. It is a pointer to the TCP/IP socket stream. If the server has written some data to it, when the client starts reading from this stream it will read only the data that is available on the socket and block if you attempt to read more data than what is currently available until the server writes more data to the socket.

So if we assume that the server has already written 5 bytes to the socket, if you attempt to read 5 bytes from the stream on the client, you will be able to retrieve those 5 bytes, but if you attempt to read 6 bytes the read operation will block until the server sends one more byte or until it times out.

zx81
  • 41,100
  • 9
  • 89
  • 105
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-1

If you look at that example you'll see that you need to call .GetResponse() first, which will answer your question.

Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28
  • Okay, fair enough. But lets say we want to consume data as it comes along the wire. Is that possible in C#? If not is that possible in any language? – The Muffin Man Jul 08 '12 at 08:07
  • When you call `GetResponseStream` does it simply get some type of handle to it, and only once you read the stream does it send each individual byte, or does calling that method download it, and once that's finished you are able to interact with it? Until you can provide a useful answer or delete this one I'm down voting this. – The Muffin Man Aug 16 '12 at 03:20