title: HttpClient: Conditionally set AcceptEncoding compression at runtime
body: <p>We are trying to implement user-determined (on a settings screen) optional gzip compression in our client which uses <code>HttpClient</code>, so we can log and compare performance across a number of different calls over a period of time. Our first attempt was to simply conditionally add the header as follows:</p>
  
  <pre><code>HttpRequestMessage request = new HttpRequestMessage(Method, Uri);
  if (AcceptGzipEncoding)
  {
       _client.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
  }
  
  //Send to the server
  result = await _client.SendAsync(request);
  
  //Read the content of the result response from the server
  content = await result.Content.ReadAsStringAsync();
  </code></pre>
  
  <p>This created the correct request, but the gzipped response was not decompressed on return, resulting in a garbled response.  I found that we had to include the <code>HttpClientHandler</code> when constructing the <code>HttpClient</code>:</p>
  
  <pre><code>HttpClient _client = new HttpClient(new HttpClientHandler
      { 
          AutomaticDecompression = DecompressionMethods.GZip
      });
  </code></pre>
  
  <p>This all works well, but we'd like to change whether the client sends the <code>Accept-Encoding: gzip</code> header <em>at runtime</em>, and there doesn't appear to be any way to access or change the <code>HttpClientHandler</code> after it's passed to the <code>HttpClient</code> constructor.  In addition, altering the headers of the <code>HttpRequestMessage</code> object doesn't have any effect on the headers of the request if they are defined by the <code>HttpClientHandler</code>.</p>
  
  <p>Is there any way to do this without recreating the <code>HttpClient</code> each time this changes?</p>
  
  <p>Edit: I've also tried to modify a reference to the <code>HttpClientHandler</code> to change <code>AutomaticDecompression</code> at runtime, but that's throwing this exception:</p>
  
  <blockquote>
    <p>This instance has already started one or more requests. Properties can only be modified before sending the first request.</p>
  </blockquote>