Search code examples
c#.netstreamreaderwebresponse

c# .NET Fast (realtime) webresponse reading


I got code, that sending GET request and recieves answer in stream. I read stream with streamreader to end. Here is code:

HttpWebRequest requestGet = (HttpWebRequest)WebRequest.Create(url);
requestGet.Method = "GET";
requestGet.Timeout = 5000;
HttpWebResponse responseGet = (HttpWebResponse)requestGet.GetResponse();
StreamReader reader = new StreamReader(responseGet.GetResponseStream());
StringBuilder output = new StringBuilder();
output.Append(reader.ReadToEnd());
responseGet.Close();

But i dont like that program is waiting until all data recieved before starting working with response. It would be great if i can do it like this(pseudocode):

//here sending GET request
do
{
response.append(streamPart recieved);
//here work with response
} while (stream not ended)

I tried streamReader.Read(char[], int32_1, int32_2), but i cant specify int32_2, becouse i dont know how many symbols i recieved. And if i use ReadToEnd - it waits for all response to load.


Solution

  • Read takes a char[] buffer and returns the number of characters read from the stream. Here's an example of how to read the response in chunks:

        public static void Main(string[] args)
        {
            var req = WebRequest.Create("http://www.google.com");
            req.Method = "GET";
            req.Timeout = 5000;
            using (var response = req.GetResponse())
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                char[] buffer = new char[1024];
                int read = 0;
                int i = 0;
                do
                {
                    read = reader.Read(buffer, 0, buffer.Length);
                    Console.WriteLine("{0}: Read {1} bytes", i++, read);
                    Console.WriteLine("'{0}'", new String(buffer, 0, read));
                    Console.WriteLine();
                } while(!reader.EndOfStream);
            }
        }
    

    I didn't see any extraneous white space or repeated data.