为什么我得到的endOfStream在WebRequest的,如果它是一个持久(保持活动)连接? [英] Why do I get to the endOfStream in a webrequest, if it is a persistent (keepalive) connection?

查看:143
本文介绍了为什么我得到的endOfStream在WebRequest的,如果它是一个持久(保持活动)连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有createst持久(保持活动)连接到服务器,例如一个WebRequest的:

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}

我不知道为什么过了一段时间responseStream.EndOfStream为真。我会认为,因为这是一个持久连接,流将永远不会关闭?

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?

推荐答案

我想你混淆保持与保持响应流开开的TCP连接。 TCP连接是底层的传输介质,而请求和响应个别实体通过该连接传输。

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.

这篇关于为什么我得到的endOfStream在WebRequest的,如果它是一个持久(保持活动)连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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