如何监听保持连接状态的HttpResponseMessage直到关闭? [英] How to listen to a keep-alive HttpResponseMessage until it's closed?

查看:125
本文介绍了如何监听保持连接状态的HttpResponseMessage直到关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HttpClient 将异步POST请求发送到远程Web服务器。该远程Web服务器将Connection标头设置为保持活动来响应。最终,它将关闭连接。

I'm using HttpClient to send an asynchronous POST request to a remote web server. That remote web server responds with the Connection header set to keep-alive. Eventually, it will close the connection.

我不知道该怎么做,就是继续接收数据,直到连接设置为关闭。

What I can't figure out how to do is keep receiving data until the connection is set to close.

例如,考虑以下代码:

HttpClient client = new HttpClient();
string requestUri = ...; //My GET uri
string content = ...; //url-encoded contents of the request

HttpResponseMessage response = await client.PostAsync(requestUri, new StringContent(content));

在上面的代码中,响应将具有连接标头设置为保持活动。我在 HttpResponseMessage 上找不到任何成员在收到第一个保持活动之后,似乎可以给我任何继续接收信息的方式。

In the code above, response will have the Connection header set to keep-alive. I couldn't find any members on HttpResponseMessage that seem to give me any means of continuing to receive information after the first keep-alive is received.

在连接关闭之前,我不知道如何继续接收响应。

我能够找到处理发送的各种资源保持活动请求。那不是我的问题。我的问题是从服务器接收/处理保持活动响应。

I was able to find all sorts of resources that dealt with sending keep-alive requests. That's not my issue. My issue is in receiving/handling a keep-alive response from the server.

请告诉我是否存在就是我可以提供以改善此问题的信息。

Please let me know if there is anymore information that I could provide to improve this question.

推荐答案

连接:保持活动的目的不是使服务器能够为同一单个请求发送多个响应-这从根本上违反了HTTP的工作原理。

Connection: keep-alive purpose is not to enable server to send multiple responses for the same single request - this would fundamentally violate how HTTP works.

连接:keep-alive 是服务器向客户端发送信号的方式,客户端可以使用相同的,已经建立的TCP连接来提交下一个请求,从而有助于避免建立新连接(以及可能的TLS握手)的成本

Connection: keep-alive is server's way of signaling to the client that client can submit next request using the same, already established, TCP connection, thus help avoid the costs of establishing new connection (and possibly TLS handshake as well).

连接的重用(通常)由底层库处理,并且被抽象掉了,这样库的用户(如开发人员)就不必担心

Reuse of the connection is (typically) handled by underlying library, and is abstracted away such that user (as in developer) of the library need not worry about handling.

您应该做的就是继续发送尽可能多的 client.Post/Get/etc 请求,一种nd HttpClient 库将负责管理基础TCP连接及其重用。

All you should be doing is keep making as many client.Post/Get/etc requests, and the HttpClient library will take care of managing the underlying TCP connection and its reuse.

其中有一个区域可能 look 与上述内容相反的HTTP令人困惑,但事实并非如此。这种情况是服务器返回HTTP状态 100继续。在这种情况下,您将看到来自服务器的另一个响应仍然是相同的原始请求(通过查看,我的意思是不是来自 HttpClient ,而是在线上,如果您被诸如WireShark或代理之类的工具监听)。在这种情况下,如果客户端发出了一个大请求,则服务器会简单地向客户端发出信号,表示它正在继续接受和读取请求,并让客户端继续发送该原始请求。收到完整请求后,服务器将处理并以最终响应代码和消息进行响应。再一次, HttpClient 将抽象出此临时 100 Continue 响应,因此作为开发人员,您无需担心(在大多数情况(如果不是全部)。

There is one area in HTTP that may look counter to the above and thus confusing, but is not. The case is when server returns HTTP Status 100 Continue. In this case you will see another response come from the server for still the same original request (and by "see" I mean not from HttpClient, but rather on the wire if you were to snoop with a tool like WireShark or a proxy). What is happening in this case is if a client is making a large request, the server simply signals to the client that it is continuing to accept and read request, and for the client to continue sending that original request. Once full request is received, the server will process and respond with a final response code and message. And again, HttpClient will abstract away this interim 100 Continue response, so as developer you need not worry about it (in most if not all cases).

这篇关于如何监听保持连接状态的HttpResponseMessage直到关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆