0

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.

Lokley
  • 35
  • 1
  • 7
  • Using some constant length shouldn't cause any problems, it should just return how ever many are left if less than length. Can you elaborate why you need to know the response length before reading? – mrtig Sep 09 '13 at 00:55
  • I tried to use 1000 as int32_2, but it returned me string with huuuuge white space after text. And also, it moves "cursor" in stream, so, i will not get what will appear in this skipped whitespace when its loaded. (gonna recheck now abaut cursor, not sure) – Lokley Sep 09 '13 at 01:00
  • Checked it up. if i use 10000 as int32_2 i get huge whitespaces(wich i can deal with and huge part of repeated text(wich is bad). – Lokley Sep 09 '13 at 01:14
  • @Lokley there is no overload of `Read` that takes a `string` argument. It is a `char[]` that acts as a buffer. Also `Read` returns the number of characters read from the stream which is tells you what part of the buffer is occupied with new data. – Mike Zboray Sep 09 '13 at 01:16
  • yes, ofcourse its char[], fixed it in main post. I create new buffer each time i append data, so it should be only new data there, but i will try to use number of characters read to append only part of buffer. – Lokley Sep 09 '13 at 01:30

1 Answers1

0

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.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Yes, its working. Probably problem was in part when i append ALL buffer instead of appending part that has been get by Read. Thank you. – Lokley Sep 09 '13 at 01:39