title: Send large file via HttpClient
body: <p>I need to upload large files (~200MB) over HTTP protocol. I want to avoid loading the files to memory and want to send them directly. </p>
  
  <p>Thanks to this <a href="https://www.techcoil.com/blog/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c/" rel="noreferrer">article</a> I was able to make it with <code>HttpWebRequest</code>.</p>
  
  <pre><code>HttpWebRequest requestToServer = (HttpWebRequest)WebRequest.Create("....");
  
  requestToServer.AllowWriteStreamBuffering = false;
  requestToServer.Method = WebRequestMethods.Http.Post;
  requestToServer.ContentType = "multipart/form-data; boundary=" + boundaryString;
  requestToServer.KeepAlive = false;
  requestToServer.ContentLength = ......;
  
  using (Stream stream = requestToServer.GetRequestStream())
  {
      // write boundary string, Content-Disposition etc.
      // copy file to stream
      using (var fileStream = new FileStream("...", FileMode.Open, FileAccess.Read))
      {
          fileStream.CopyTo(stream);
      }
  
      // add some other file(s)
  }
  </code></pre>
  
  <p>However, I would like to do it via <code>HttpClient</code>. I found <a href="http://www.tugberkugurlu.com/archive/streaming-with-newnet-httpclient-and-httpcompletionoption-responseheadersread" rel="noreferrer">article</a> which describes using of <code>HttpCompletionOption.ResponseHeadersRead</code> and I tried something like this but it does not work unfortunately.</p>
  
  <pre><code>WebRequestHandler handler = new WebRequestHandler();
  
  using (var httpClient = new HttpClient(handler))
  {
      httpClient.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; boundary=" + boundaryString);
      httpClient.DefaultRequestHeaders.Add("Connection", "close");
  
      var httpRequest = new HttpRequestMessage(HttpMethod.Post, "....");
  
      using (HttpResponseMessage responseMessage = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead))
      {
          using (Stream stream = await responseMessage.Content.ReadAsStreamAsync())
          {
              // here I wanted to write content to the stream, but the stream is available only for reading
          }
      }
  }
  </code></pre>
  
  <p>Maybe I overlooked or missed something...</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>On top of it, it is important to use <code>StreamContent</code> with proper headers:</p>
  
  <ul>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition" rel="noreferrer">Content-Disposition</a></li>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type" rel="noreferrer">Content-Type</a></li>
  </ul>