title: Download .xls file from a url
body: <p>I'm trying to download an xls file from a url: 
  <a href="http://www.site.com/ff/excel/file.aspx?deven=0" rel="nofollow">http://www.site.com/ff/excel/file.aspx?deven=0</a></p>
  
  <p>I'm using this code but when the download is complete the file is not properly downloaded. How can I download this file correctly?</p>
  
  <pre><code>string remoteFilename="http://www.site.com/ff/excel/file.aspx?deven=0";
  string localFilename = "D:\\1\\1.xls";
  Stream remoteStream = null;
  Stream localStream = null;
  WebResponse response = null;
  
  try
  {
      // Create a request for the specified remote file name
      WebRequest request = WebRequest.Create(remoteFilename);
      if (request != null)
      {
          // Send the request to the server and retrieve the
          // WebResponse object 
          response = request.GetResponse();
          response.ContentType = "application/vnd.ms-excel";
  
          if (response != null)
          {
              // Once the WebResponse object has been retrieved,
              // get the stream object associated with the response's data
              remoteStream = response.GetResponseStream();
  
              // Create the local file
              localStream = File.Create(localFilename);
  
              // Allocate a 1k buffer
              byte[] buffer = new byte[1024];
              int bytesRead;
  
              // Simple do/while loop to read from stream until
              // no bytes are returned
              do
              {
                  // Read data (up to 1k) from the stream
                  bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
  
                  // Write the data to the local file
                  localStream.Write(buffer, 0, bytesRead);
  
                  // Increment total bytes processed
  
              } while (bytesRead &gt; 0);
          }
      }
  }
  catch (Exception e)
  {
      MessageBox.Show(e.Message);
  }
  finally
  {
      // Close the response and streams objects here 
      // to make sure they're closed even if an exception
      // is thrown at some point
      if (response != null) response.Close();
      if (remoteStream != null) remoteStream.Close();
      if (localStream != null) localStream.Close();
  }
  MessageBox.Show("file downl");
  </code></pre>