HttpWebResponse.GetResponseStream (): when is the response body sent?

I use the System.Net.HttpWebRequest class to implement a simple HTTP downloader that can be paused, canceled, and even resumed after it has been canceled (with the HTTP range request header).

It is clear that HttpWebRequest.GetResponse () is when an HTTP request is actually sent to the server, and the method returns when an HTTP response is received (or a timeout occurs). However, the response body is presented by Stream, which leaves me with the question whether the response body is really transmitted with the response header (i.e. it is already loaded when GetResponse () returns) or is it loaded only by request when I try to read from the response stream ? Or maybe when I call the HttpWebResponse.GetResponseStream () method?

Unfortunately, the msdn documentation is not listed, and I don't know enough about the HTTP protocol to be able to tell.

How transfers are transferred, etc. in this case (i.e. how should I handle them in my C # application)? When is the response data actually downloaded from the server?

+4
source share
1 answer

It all depends on TCP, the underlying HTTP protocol. The way TCP works is that data is sent in segments. Whenever a client sends a segment to a server, among the data sent there is information about how much additional data it is ready to receive. Usually this corresponds to some buffer on the client side. When a client receives some data, it also sends a segment to the server, confirming the received data.

So, assuming that the client processes the received data very slowly, the sequence of events may look like this:

  • A connection is established, clients report how much data it is ready to receive.
  • The server sends one or more segments to the client, the total amount of data in them does not exceed the client number, which said that it is ready to accept
  • The client tells the server: I received the data that you sent me, but do not send me now.
  • The client is processing some data.
  • The client tells the server: you can send me more bytes of data

What does this mean with respect to GetResponse() ? When you call GetResponse() , the client sends a request, reads the HTTP response header (which usually fits into one segment, but may be more) and returns. At this point, if you do not start reading the response stream (which you receive by calling GetResponseStream() ), some data from the server is received, but only to fill the buffer. When this is full, no more data is transferred until you start reading the response stream.

+5
source

All Articles