title: Get response for HttpCompletionOption ResponseHeadersRead
body: <p>I am trying to get progress of an api along with the response. <code>ResponseHeadersRead</code> works fine to get the progress but I can't figure out why it doesn't return the response.</p>
  
  <p><strong>Download part</strong></p>
  
  <pre><code>public async Task StartDownload()
  {
      _httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1), };
  
      using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead))
      {
          await DownloadFileFromHttpResponseMessage(response);
  
          string strResp = await response.Content.ReadAsStringAsync();
          Debug.WriteLine(strResp); // Doesn't print anything
      }
  }
  </code></pre>
  
  <p><strong>Reading Stream part</strong></p>
  
  <pre><code>private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
  {
      response.EnsureSuccessStatusCode();
  
      var totalBytes = response.Content.Headers.ContentLength;
  
      using (var contentStream = await response.Content.ReadAsStreamAsync())
      {
          await ProcessContentStream(totalBytes, contentStream);
  
      }
  }
  </code></pre>
  
  <p>The code is actually from <a href="https://stackoverflow.com/a/43169927/3480088">another answer</a>.</p>
  
  <p>I am just not getting the response. If I use <code>ResponseContentRead</code> I get response but it defeats the purpose of progress.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>ProcessContentStream code</strong> - This part read the response as it comes bit by bit and posts the progress in <code>TriggerProgressChanged</code></p>
  
  <pre><code>private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
  {
      var totalBytesRead = 0L;
      var readCount = 0L;
      var buffer = new byte[8192];
      var isMoreToRead = true;
  
      using (var fileStream = new FileStream(DestinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
      {
          do
          {
              var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
              if (bytesRead == 0)
              {
                  isMoreToRead = false;
                  //TriggerProgressChanged(totalDownloadSize, totalBytesRead);
                  continue;
              }
  
              await fileStream.WriteAsync(buffer, 0, bytesRead);
  
              totalBytesRead += bytesRead;
              readCount += 1;
  
              if (readCount % 100 == 0)
              {
                  //TriggerProgressChanged(totalDownloadSize, totalBytesRead);
              }
          }
          while (isMoreToRead);
      }
  }
  </code></pre>
  
  <p><strong>Post the progress</strong></p>
  
  <pre><code>private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead)
  {
      if (ProgressChanged == null)
          return;
  
      double? progressPercentage = null;
      if (totalDownloadSize.HasValue)
          progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2);
  
      ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
  }
  </code></pre>
  
  <p><code>ProgressChanged</code> is a delegate method.</p>
  
  <p><a href="https://github.com/rishabdutta/Progress" rel="nofollow noreferrer">Project link</a></p>