0

I'm using an HttpWebResponse object to make an HTTP Request. I then open a StreamReader object to read in the text. If I use stream.Read(charArr, 0, 10000), less than 10000 characters are read in. When I use stream.ReadToEnd(), all 45+ million characters are returned so I know there is more than 10000 characters in the stream. Here is a snippet of my code:

using(StreamReader stream = new StreamReader(httpResponse.getResponseStream()))
{
    int charsToRead = 10000;
    char[] charArr = new char[charsToRead];
    int charsRead = stream.Read(charArr, 0, charsToRead);
    //charsRead is never 10000 even though the response stream is over 45,000,000 lines long
}

Any ideas on why the StreamReader.Read method won't read in the number of characters I tell it to would be greatly appreciated.

cas4
  • 321
  • 1
  • 11
  • 1
    Use ReadBlock instead of Read. This is what it is designed for. – Mike Zboray Apr 16 '18 at 18:57
  • 1
    Thanks, that works, and the SO question also partially explained why: https://stackoverflow.com/questions/592244/difference-between-streamreader-read-and-streamreader-readblock – cas4 Apr 16 '18 at 19:08

0 Answers0