【发布时间】:2014-07-11 05:13:52
【问题描述】:
我正在构建一个小型 Windows 应用程序来使用 POST 请求。下面的代码适用于 GET 请求和第一个 POST 请求。基本上,当我阅读 POST DATA 时,它第一次(或前几次)运行良好。过了一会儿(几秒钟 - 它挂起。任何传入的请求都挂起。有什么想法吗?假设内容长度是正确的。
while (true)
{
System.Console.WriteLine("The server is running at port 8001...");
System.Console.WriteLine("Waiting for a connection.....");
TcpClient client = _listener.AcceptTcpClient();
int incomingDataLength = client.ReceiveBufferSize;
Stream ist = client.GetStream();
BufferedStream bst = new BufferedStream(ist);
int k = 0;
String line = ReadLine(bst);
System.Console.WriteLine(line);
while ((line = ReadLine(bst)) != null)
{
if (line == "") break;
System.Console.WriteLine(line);
}
MemoryStream ms = new MemoryStream();
int contentLen = 3429;
//if (this.HttpHeaders.ContainsKey("Content-Length"))
{
//content_len = Convert.ToInt32(this.HttpHeaders["Content-Length"]);
byte[] buf = new byte[4096];
int to_read = content_len;
while (to_read > 0)
{
int numread = bst.Read(buf, 0, Math.Min(buf.Length, to_read));
if (numread == 0)
{
if (to_read == 0) break;
else throw new Exception("client disconnected during post");
}
to_read -= numread;
ms.Write(buf, 0, numread);
}
ms.Seek(0, SeekOrigin.Begin);
}
using (StreamReader sr = new StreamReader(ms))
{
System.Console.WriteLine(sr.ReadToEnd());
}
bst.Close();
client.Close();
ReadLine 是
private String ReadLine(Stream stream)
{
int k;
StringBuilder lineBuilder = new StringBuilder();
while (true)
{
k = stream.ReadByte();
if (k < 0) continue;
char c = Convert.ToChar(k);
if (c == '\n') break;
if (c == '\r') continue;
lineBuilder.Append(c);
}
return lineBuilder.ToString();
}
【问题讨论】:
-
什么是
ReadLine?暂停调试器时它挂在哪里?对 bst 和客户端使用using块。 -
不用担心使用块。它们不相关。这段代码有一百万件事情可能会更好,但我对它为什么会阻塞很感兴趣。
-
它在 ReadLine 中阻塞
-
无法复制。 postimg.org/image/7tqcsth3z我错过了什么?你没有包括什么?你的测试客户是什么?看起来它不再发送任何数据。坏了。
-
测试客户端是高级 REST 客户端(Chrome 扩展)。尝试运行 POST 请求。等待几分钟,然后再次运行。
标签: c# http tcp tcplistener