I receive a JSON from an http stream
I rely on the Content-Length header to read the body, but the StreamReaders reads extra characters
is it possible to use the Content-Length value of the header as-is while using a StreamReader ?
with this function, there are extra characters after the JSON which makes it unparsable
string ReceiveJSON(StreamReader input, int msgSize)
{
var buffer = new char[msgSize];
int offset = 0;
var toRead = msgSize;
while (toRead > 0)
{
var charsRead = await input.ReadAsync(buffer, offset, toRead);
offset += charsRead;
toRead -= charsRead;
}
return new string(buffer, 0, msgSize);
}
but this function does not add extra characters after the JSON
void ReceiveJSON(Stream input, int msgSize)
{
var buffer = new byte[msgSize];
int offset = 0;
while(msgSize > 0)
{
var readSize = await input.ReadAsync(buffer, offset, msgSize);
offset += readSize;
msgSize -= readSize;
}
return Encoding.UTF8.GetString(buffer);
}
I am requested to use the StreamReader, but
how can I solve the first function so it reads the right amount of bytes ?
thanks for your help