title: HttpWebRequest - sending file by chunks and tracking progress
body: <p>I have the following code that uploads the file to the server by making the POST request:</p>
  
  <pre><code>string fileName = @"C:\Console_sk.txt";
  
  HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create("http://" + Environment.MachineName + ":8000/Upload");
  request.Method = "POST";
  request.ContentType = "text/plain";
  request.AllowWriteStreamBuffering = false;
  
  Stream fileStream = new FileStream(fileName, FileMode.Open);
  request.ContentLength = fileStream.Length;
  
  Stream serverStream  = request.GetRequestStream();
  
  byte[] buffer = new byte[4096];
  while (true)
  {
      int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
      if (bytesRead &gt; 0)
      {
          serverStream.Write(buffer, 0, bytesRead);     
      }
      else
      {
          break;
      }
  }
  
  serverStream.Close();
  fileStream.Close();
  
  request.GetResponse();
  </code></pre>
  
  <p>This will be used to upload potentially large files, therefore it is essential for us to track the progress of this upload. </p>
  
  <p>Based on <a href="http://social.msdn.microsoft.com/Forums/eu/netfxnetcom/thread/6ead3249-c31b-49b5-bded-8ea182b81607" rel="nofollow">various sources</a>, I've thought that my code will upload file in 4096 byte chunks i.e. multiple POST requests will be made. But when tracking requests by using Fiddler, it shows that only one POST request is made. On other hand, when I enabled <a href="http://msdn.microsoft.com/en-us/library/ty48b824.aspx" rel="nofollow">.NET network tracing</a>, the resulting log showed that 4096 byte chunks are indeed written to request stream and the socket is always being opened for each write operation.</p>
  
  <p>My understanding of networking in general is quite poor, so I don't quite understand how this really works. When calling <code>serverStream.Write</code>, is the provided chunk really sent over the network, or is it just buffered somewhere? And when <code>serverStream.Close</code> is called, can I be sure that the whole file has been uploaded? </p>