D3vy
D3vy

Reputation: 966

Reading HTTP Response as it comes in

A client has a page which when called starts a long running process and intermittently spits out its progress as it goes

In the format

[dd/MM/yyyy hh:mm:ss] - Process Started
[dd/MM/yyyy hh:mm:ss] - CSV Imported
[dd/MM/yyyy hh:mm:ss] - Process 10% complete

Then 30 seconds later it might write out :

[dd/MM/yyyy hh:mm:ss] - User x Created
[dd/MM/yyyy hh:mm:ss] - User y Created
[dd/MM/yyyy hh:mm:ss] - Process 20% complete

etc... it takes 10-20 minutes to run, we dont have access to the code for this page.

What I have been asked to do is to call this page from one of the other applications, consume the output and give a realtime update on our dashboard.

My first thought was was to use an http client call .GetStreamAsync() and have a loop reading the stream intermittently and reporting back on the last thing that was written out.

This was my first attempt :

using (HttpClient httpClient = new HttpClient())
            {

                httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                var requestUri = "http://localhost:64501/Page1.aspx";
                var stream = httpClient.GetStreamAsync(requestUri).Result;

                using (var reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        var currentLine = reader.ReadLine();
                        Thread.Sleep(1000);
                    }
                }
            }

However var currentLine = reader.ReadLine();

Appears to block and wait for the response to complete before returning anything..

I need to be able to read the stream as it comes in.. Is this possible?

Upvotes: 2

Views: 3631

Answers (1)

Gusman
Gusman

Reputation: 15151

The problem lies in ReadLine, the server may be not sending lines (something logic as it seems to be prepared to be sent to a web page where newlines are ignored) so you need to read chuncks of data and convert those to strings:

        using (HttpClient httpClient = new HttpClient())
        {

            httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
            var requestUri = "http://localhost:64501/Page1.aspx";
            var stream = httpClient.GetStreamAsync(requestUri).Result;

            string read = "";
            byte[] buffer = new byte[1024];

            while(!stream.EndOfStream)
            {
                int readed = stream.Read(buffer, 0, 1024);
                read += Encoding.UTF8.GetString(buffer, 0, readed);

                //Do whatever you need to do with the string.
            }
        }

Upvotes: 3

Related Questions