简体   繁体   English

HttpWebResponse.GetResponseStream():什么时候传输响应体?

[英]HttpWebResponse.GetResponseStream(): when is the response body transmitted?

I'm using the System.Net.HttpWebRequest class to implement a simple HTTP downloader that can be paused, canceled and even resumed after it was canceled (with the HTTP Range request header).我正在使用System.Net.HttpWebRequest class 来实现一个简单的 HTTP 下载器,它可以在取消后暂停、取消甚至恢复(使用 Z293C9EA246FF9985DC6F62A650F7898Z 范围请求标头)。

It's clear that HttpWebRequest.GetResponse() is when the HTTP request is actually sent to the server, and the method returns when a HTTP response is received (or a timeout occurs).很明显 HttpWebRequest.GetResponse() 是在 HTTP 请求实际发送到服务器时,该方法在收到 HTTP 响应时返回(或发生超时)。 However, the response body is represented with a Stream, which leaves me wonder whether the response body is actually transmitted with the response header (ie it's already downloaded when GetResponse() returns), or is it only downloaded on-demand, when I try to read from the response stream?但是,响应正文用 Stream 表示,这让我想知道响应正文是否实际上是与响应 header 一起传输的(即它在 GetResponse() 返回时已经下载),或者它只是在我尝试时按需下载从响应 stream 中读取? Or maybe when I call the HttpWebResponse.GetResponseStream() method?或者当我调用 HttpWebResponse.GetResponseStream() 方法时?

Unfortunately the msdn documentation doesn't tell, and I don't know enough about the HTTP protocol to be able to tell.不幸的是,msdn 文档没有说明,而且我对 HTTP 协议的了解还不够,无法说明。

How do chunked transfers and the like behave in this case (that is, how should I handle them in my C# application)?在这种情况下,分块传输等行为如何(也就是说,我应该如何在我的 C# 应用程序中处理它们)? When is actually the response data downloaded from the server?实际上什么时候从服务器下载响应数据?

This all depends on TCP, the underlying protocol of HTTP.这一切都依赖于HTTP的底层协议TCP。 The way TCP works is that data is sent in segments. TCP 的工作方式是分段发送数据。 Whenever a client sends a segment to the server, among the data sent is information about how much additional data is it ready to receive.每当客户端向服务器发送段时,发送的数据中都会包含有关它准备接收多少附加数据的信息。 This usually corresponds to some kind of buffer on the client's part.这通常对应于客户端的某种缓冲区。 When the client receives some data, it also sends a segment to the server, acknowledging the received data.当客户端接收到一些数据时,它也会向服务器发送一个段,确认接收到的数据。

So, assuming the client is very slow in processing the received data, the sequence of events could go like this:因此,假设客户端在处理接收到的数据时非常慢,事件序列可以像这样 go :

  1. Connection is established, the clients says how much data is it ready to receive.连接建立,客户端说它准备接收多少数据。
  2. Server sends one or more segments to the client, the total data in them at most the amount client said it is ready to receive服务器向客户端发送一个或多个段,其中的总数据最多为客户端表示准备接收的数量
  3. Client says to the server: I received the data you sent me, but don't send me anymore for now.客户端对服务器说:我收到了你发给我的数据,但现在不要再发给我了。
  4. Client processes some of the data.客户端处理一些数据。
  5. Client says to the server: You can send me x more bytes of data客户端对服务器说:你可以给我发送 x 更多字节的数据

What does this mean with regards to GetResponse() ?这对GetResponse()意味着什么? When you call GetResponse() , the client sends the request, reads the HTTP header of the response (which usually fits into one segment, but it may be more) and returns.当您调用GetResponse()时,客户端发送请求,读取响应的 HTTP header (通常适合一个段,但可能更多)并返回。 At this point, if you don't start reading the response stream (that you get by calling GetResponseStream() ), some data from the server is received, but only to fill the buffer.此时,如果您不开始读取响应 stream(您通过调用GetResponseStream()获得),则会收到来自服务器的一些数据,但仅用于填充缓冲区。 When that is full, no more data is transmitted until you start reading the response stream.当它已满时,在您开始读取响应 stream 之前不会再传输数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpWebResponse.GetResponseStream转换&lt;代替&lt;etc - HttpWebResponse.GetResponseStream converts &lt; instead of < etc HttpWebResponse.GetResponseStream()(似乎是)失败,没有异常 - HttpWebResponse.GetResponseStream() (seems to be) failing with no exception 无法将HttpWebResponse.GetResponseStream()分配给Stream - Unable to assign HttpWebResponse.GetResponseStream() to Stream 从c#中的HTTPWebResponse.GetResponseStream()上传到S3 - Upload to S3 from HTTPWebResponse.GetResponseStream() in c# HttpWebResponse.GetResponseStream()。Length引发异常,即使结果为200 OK状态 - HttpWebResponse.GetResponseStream().Length throws exception even result is 200 OK status System.Net.HttpWebResponse.GetResponseStream()在WebException中返回截断的主体 - System.Net.HttpWebResponse.GetResponseStream() returns truncated body in WebException HttpWebResponse GetResponseStream 挂在 Dispose - HttpWebResponse GetResponseStream hanging in Dispose 使用HttpWebResponse :: GetResponseStream方法时接收不完整的数据 - Receiving incomplete data when using HttpWebResponse::GetResponseStream method 为什么 Stream.CanRead 在 HttpWebResponse object 上第二次调用 GetResponseStream() 时返回 false - Why does Stream.CanRead return false when calling GetResponseStream() second time on HttpWebResponse object 寻找一种方法重写HttpWebResponse的GetResponseStream方法 - Looking for a way to override the GetResponseStream Method of HttpWebResponse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM