简体   繁体   中英

Get Content of a http request

i am trying to get the content of http request...my program is using threads and sockets to comunicate to a access terminal.. i ne

this is the request sent from the terminal:

POST /iclock/devicecmd?SN=2182682370001 HTTP/1.1 and the content is ID1&Return=0&CMD=INFO ..

and this is my function to get he full content of http request:

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");

}

the buffer for now is 32 bytes. for the moment i am only getting the http header and not the content.

if i want to return the content, do i need to parse the string byte by byte..then find the value of content-length and ask to fecth x more bytes.

is this my only option?

Thanks in advance

The Available property does not what you think it does. Remove its use.

Also, you are not using the return value of Read . TCP offers you a stream of bytes. A Read operation can return any amount starting with one byte. You code must handle that case.

You should probably continue reading until the remote side is done sending. You can find out about that by checking the amount of bytes read against zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM