0

I'm reading from an SslStream as follows:

byte[] data = new byte[tcp.ReceiveBufferSize];
int bytesRead = -1;
do {
    bytesRead = stream.Read(data, 0, data.Length);

    using (MemoryStream ms = new MemoryStream(data, 0, bytesRead))
    using (StreamReader rd = new StreamReader(ms))
    {
        string returnData = rd.ReadToEnd();
        sb.Append(returnData);
    }
} while (bytesRead != 0);

It does read from the stream. However, because the actual length of the stream is unknown, the last read just freezes the app. A sample message looks like this:

HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Content-Length: 11534
Server: Jetty(6.1.x)

{long json data here...}

The server returns the length of the content, however it's not the total length of the stream. Is there an efficient way to read this stream successfully?

4

1 回答 1

0

我刚刚解决了同样的问题。我有一个缓冲区大小 = 4096,为了防止在最后一次读取时冻结,我们需要检查 SslStream.Read 是否小于缓冲区大小。如果为真,我们打破读取循环,否则,我们继续阅读。

于 2015-06-04T02:26:39.573 回答