title: Downloading a webpage in C# example
body: <p>I am trying to understand some example code on this web page: (<a href="http://www.csharp-station.com/HowTo/HttpWebFetch.aspx" rel="nofollow noreferrer"><a href="http://www.csharp-station.com/HowTo/HttpWebFetch.aspx" rel="nofollow noreferrer">http://www.csharp-station.com/HowTo/HttpWebFetch.aspx</a></a>) that downloads a file from the internet. </p>
  
  <p>The piece of code quoted below goes through a loop getting chunks of data and saving them to a string until all the data has been downloaded. As I understand it, "count" contains the size of the downloaded chunk and the loop runs until count is 0 (an empty chunk of data is downloaded).</p>
  
  <p>My question is, isn't it possible that count could be 0 without the file being completely downloaded? Say if the network connection is interrupted, the stream may not have any data to read on a pass of the loop and count should be 0, ending the download prematurely. Or does ResStream.Read stop the program until it gets data? Is this the correct way to save a stream?</p>
  
  <pre>
      int count = 0;
  
        do
        {
         // fill the buffer with data
         count = resStream.Read(buf, 0, buf.Length);
  
         // make sure we read some data
         if (count != 0)
         {
          // translate from bytes to ASCII text
          tempString = Encoding.ASCII.GetString(buf, 0, count);
  
          // continue building the string
          sb.Append(tempString);
         }
        }
        while (count > 0); // any more data to read?
  
  </pre>