如果是持久(保持活动)连接,为什么我会在 Web 请求中到达 endOfStream?

发布于 2024-09-06 20:09:00 字数 1332 浏览 5 评论 0原文

我有一个网络请求,它创建与服务器的持久(保持活动)连接,例如:

webRequest.ContentType = "application/x-www-form-urlencoded";
                    // Set the ContentLength property of the WebRequest.
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.Timeout = Timeout.Infinite;
                    webRequest.KeepAlive = true;
                    webRequest.ReadWriteTimeout = Timeout.Infinite;
                    //[System.Threading.Timeout]::Infinite

                    webRequest.UserAgent = "www.socialblazeapp.com";
                    Stream dataStream = webRequest.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();
                    // Get the response.
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                    responseStream = new StreamReader(webResponse.GetResponseStream(), encode);
                    while(!responseStream.EndOfStream){ //do something}

我想知道为什么responseStream.EndOfStream 一段时间后变为true。我会假设因为这是一个持久连接,所以流永远不会关闭?

有什么想法为什么会发生这种情况吗?

I have a webrequest that createst a persistent (keepalive) connection to the server, e.g.:

webRequest.ContentType = "application/x-www-form-urlencoded";
                    // Set the ContentLength property of the WebRequest.
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.Timeout = Timeout.Infinite;
                    webRequest.KeepAlive = true;
                    webRequest.ReadWriteTimeout = Timeout.Infinite;
                    //[System.Threading.Timeout]::Infinite

                    webRequest.UserAgent = "www.socialblazeapp.com";
                    Stream dataStream = webRequest.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();
                    // Get the response.
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                    responseStream = new StreamReader(webResponse.GetResponseStream(), encode);
                    while(!responseStream.EndOfStream){ //do something}

I'm wondering why responseStream.EndOfStream becomes true after a while. I would have assumed that because this is a persistent connection, the stream would never close?

Any ideas why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-09-13 20:09:00

我认为您混淆了保持 TCP 连接打开和保持响应流打开。 TCP 连接是底层传输介质,而请求和响应是通过该连接进行通信的单独实体。

通过持久连接,您[理论上]可以通过同一连接发出多个请求/响应对。如果没有持久连接,您实际上将打开连接,发出请求,接收响应,然后关闭连接,然后对后续请求/响应对重复该过程。

然而,响应本身的大小是有限的,一旦您收到完整的响应,流应该关闭,因为没有什么可以告诉您的了。一旦您发出另一个请求,就会出现另一个响应;我不清楚.Net 是否会重用底层的持久连接。

I think you're confusing keeping the TCP connection open with keeping the response stream open. The TCP connection is the underlying transmission medium, whereas the request and response are individual entities communicated via that connection.

With a persistent connection you [in theory] could issue multiple request/response pairs across the same connection. Without a persistent connection you would essentially open the connection, issue the request, receive the response, then close the connection and then repeat that process for subsequent request/response pairs.

The response itself however is finite in size, once you're received the completed response the stream should close as there is nothing more to tell you. Once you issue another request, another response would follow; I'm not clear as to whether .Net will reuse the underlying persistent connection.

负佳期 2024-09-13 20:09:00

要做的就是保持 TCP 连接打开。如果我没看错的话,这意味着您可以对给定服务器的多个请求重用相同的物理 TCP 连接。它不会做的是保持流打开以便服务器可以发送附加信息。

如果您确实想要流数据,您应该使用不同的协议或直接 TCP。

All that is supposed to do is keep the TCP connection open. If I'm reading this correctly, that means you can reuse the same physical TCP connection for multiple requests to a given server. What it won't do is keep your stream open so that the server can send additional information.

If you really want streaming data you should either use a different protocol or straight TCP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文