英文:
Even with 'HttpCompletionOption.ResponseHeadersRead', where does HttpClient store the response?
问题
Even with using HttpCompletionOption.ResponseHeadersRead
like this:
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://myserver.com");
var request = new HttpRequestMessage(HttpMethod.Get, "huge_file.bin");
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
As soon as SendAsync
is called, HttpClient issues a GET request. This can be seen with Fiddler.
Under the hood, where does the underlying HttpClient actually store the the request response? Is it in memory? If it's in memory, what if the response is really huge, couldn't that be problem? In my tests, it does not seem to be in memory.
Is it maybe in non-user memory, like in the kernel memory? I'm not familiar how does the network things work internally when downloading data.
Note that I'm not asking about consuming the response content by calling for example response.Content.ReadAsStringAsync()
. I'm asking how does HttpClient reads and stores the response internally, before I start to consume it by calling response.Content.ReadAsStringAsync()
for example.
英文:
Even with using HttpCompletionOption.ResponseHeadersRead
like this:
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://myserver.com");
var request = new HttpRequestMessage(HttpMethod.Get, "huge_file.bin");
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
As soon as SendAsync
is called, HttpClient issues a GET request. This can be seen with Fiddler.
Under the hood, where does the underlying HttpClient actually store the the request response? Is it in memory? If it's in memory, what if the response is really huge, couldn't that be problem? In my tests, it does not seem to be in memory.
Is it maybe in non-user memory, like in the kernel memory? I'm not familiar how does the network things work internally when downloading data.
Note that I'm not asking about consuming the response content by calling for example response.Content.ReadAsStringAsync()
. I'm asking how does HttpClient reads and stores the response internally, before I start to consume it by calling response.Content.ReadAsStringAsync()
for example.
答案1
得分: 2
不使用ResponseHeadersRead
时,它不会将响应存储在任何地方。只有在调用ReadAs...
之后,服务器才完全发送响应。
至于在Fiddler中查看请求:是的,请求已发出,但它实际上是否显示了响应中接收的所有字节?
无论如何,Fiddler是一个透明代理,可能会完全缓存请求和响应以解析它们。尝试使用Wireshark并观察实际的网络数据包,您将看到响应流在调用ReadAsStream
并实际从流中读取字节之前并没有完全读取。
这就是TCP的工作原理(HTTP运行在TCP套接字上):它在发送一定量的数据后等待ACK
。不过,它可能不仅仅是头部,因为TCP通常会在等待ACK
之前推送多个数据包。
如果您在足够长的时间内不读取数据,服务器将中断连接。
英文:
It doesn't store it anywhere if you use ResponseHeadersRead
. The response hasn't been fully sent by the server until you call ReadAs...
.
As far as seeing the request in Fiddler: yes the request is issued, but is it actually showing all the bytes being received in the response?
In any case, Fiddler is a transparent proxy, which is probably going to fully cache requests and responses in order to parse them. Try using Wireshark and watch the actual network packets, and you will see that the response stream is not fully read until you call ReadAsStream
and actually read the bytes off the stream.
That's just how TCP works (HTTP runs on TCP sockets): it waits for an ACK
after a certain amount of data. It might be more than just the headers though, as TCP commonly pushes through more than one packet before waiting for an ACK
.
If you don't read the data after a long enough amount of time, the server will just break the connection.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论