[英]C# SslStream Reading Issues
Guys i'm using SslStream as a server to test my app, but i have issues reading from the stream. 伙计们,我使用SslStream作为服务器来测试我的应用程序,但是从流中读取时出现问题。 I'm using the following code:
我正在使用以下代码:
while (true)
{
int read = sslStream.Read(buffer, 0, buffer.Length);
string bufferString = System.Text.Encoding.Default.GetString(buffer);
// Check for End?
if (bufferString.IndexOf("\n\r\n", System.StringComparison.Ordinal) != -1)
{
break;
}
}
The problem is that the first loop returns: 问题是第一个循环返回:
G\0\0\0\0\0
and the second run returns: 然后第二轮返回:
ET /whateverman
while the result should be 而结果应该是
GET /whateverman
What is the issue and is there a better way to read from an SslStream? 问题是什么,有没有更好的方法来读取SslStream?
Result is exactly as expected (and not directly related to SSL stream) - you are converting bytes that you did not read. 结果完全符合预期(并且与SSL流没有直接关系)-您正在转换未读取的字节。
If you want to manually read strings from Stream you must respect result of Read call that tells you home many bytes actually read from the stream. 如果要从Stream手动读取字符串,则必须遵守Read调用的结果,该结果告诉您从流中实际读取的许多字节。
string partialString = System.Text.Encoding.Default.GetString(buffer, 0, read);
And than don't forget to concatenate strings. 而且不要忘了连接字符串。
Note: using some sort of reader ( StreamReader
/ BinaryReader
) may be better approach. 注意:使用某种类型的阅读器(
StreamReader
/ BinaryReader
)可能是更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.