title: C# downloading a webpage. A better way needed, CPU usage high
body: <p>I'm trying to get this piece of code working a little better. I suspect it's the loop reading one byte at a time. I couldn't find another way of doing this with gzip decompression. Implementing a <code>StreamReader</code> is fine, but it returns a string which I can't pass to the decompression stream.</p>
  
  <p>Is there a better way?</p>
  
  <pre><code>byte[] bufffer = null;
  List&lt;byte&gt; resourceBytes = new List&lt;byte&gt;();
  int byteValue = 0;
  WebResource resource = new WebResource();
  HttpWebResponse webResponse = null;
  
  try {
      HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(resourceUri);
      webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
      webRequest.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
      webRequest.UserAgent = agent;
      webRequest.Accept = "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1";
      webRequest.Credentials = CredentialCache.DefaultCredentials;
      webRequest.Referer = resourceUri.OriginalString;
      webRequest.Timeout = 5000;
  
      webResponse = (HttpWebResponse)webRequest.GetResponse();
  
      Stream webStream = webResponse.GetResponseStream();
  
      if (!string.IsNullOrEmpty(webResponse.ContentEncoding)) {
          if (webResponse.ContentEncoding.ToLower().Contains("gzip")) {
              webStream = new GZipStream(webStream, CompressionMode.Decompress);
          }
          else if (webResponse.ContentEncoding.ToLower().Contains("deflate")) {
              webStream = new DeflateStream(webStream, CompressionMode.Decompress);
          }
      }
  
      do {
          byteValue = webStream.ReadByte();
  
          if (byteValue != -1) {
              resourceBytes.Add((byte)byteValue);
          }
  
      } while (byteValue != -1);
  
  
      //Free up resources
      webStream.Close();
      webResponse.Close();
  
      bufffer = resourceBytes.ToArray();
  </code></pre>