我想获得http请求的内容的内容......我的计划是使用线程,插座comunicate到接入终端.. 我得到一个http请求
甲肾上腺素这是从发送的请求终端:
POST /iclock/devicecmd?SN=2182682370001 HTTP/1.1
和内容 ID1&Return=0&CMD=INFO
..
,这是我的函数来获取http请求的,他全部内容:
private string GetPedido(NetworkStream stream)
{
string sPedido = "" ;
Byte[] bytesFromStream = new Byte[GlobalFunctionAndVariables.iStreamBufferSize];
while (_tcpClient.Available>0)
{
stream.Read(bytesFromStream, 0, bytesFromStream.Length);
//Console.Write("available: {0}\n", _tcpClient.Available);
sPedido += System.Text.Encoding.ASCII.GetString(bytesFromStream, 0, bytesFromStream.Length);
}
Console.WriteLine("Terminou, a enviar resultado \n");
}
现在的缓冲区是32字节。 目前,我只获取http标题而不是内容。
如果我想要返回内容,我是否需要按字节解析字符串字节..然后找到content-length的值并请求fecth x more bytes。
这是我唯一的选择吗?
在此先感谢
只是好奇,为什么,如果你正在处理HTTP请求,你没有使用[HttpListener](http://msdn.microsoft.com/en-us/library/system.net.httplistener(v = vs。 110)的.aspx)。 –
您是否尝试过使用StreamReader?看看这里:http://stackoverflow.com/questions/7535901/how-to-signal-end-of-data-without-closing-networkstream-in-c-sharp – Norman
更改为流阅读器,后streamreader。 readtoend(),我把线程spleep.still不工作,因为当从streamreader.readtoend()传递给thread.sleep时,客户端关闭套接字。 –