title: C# HttpClient - can I force close the connection?
body: <p>We are seeing an issue where a customer is hitting our API and cancelling the request before it completes.  We believe that they are also closing the connection so the requests are not getting written to the IIS logs.  I'm trying to create a runner to duplicate this behavior.  What I can't figure out is <strong><em>how can I force close the connection so that the server cannot send data back?</em></strong></p>
  
  <p>Here is my current code:</p>
  
  <pre><code> private static async Task RunMe()
      {
  
          var cts = new CancellationTokenSource();
          cts.CancelAfter(5000);
  
          using (var client = new HttpClient())
          {
              try
              {
                  client.DefaultRequestHeaders.ConnectionClose = true;
                  client.DefaultRequestHeaders.Add("x-apikey", "some key");
                  Console.WriteLine("making request");
                  using (var response = await client.GetAsync("https://foo.com/api/apitest/testcancel", cts.Token))
                  {
                      var result = await response.Content.ReadAsStringAsync();
                      Console.WriteLine($"Request completed: {result}");
                  }
              }
              finally
              {                    
                  client.CancelPendingRequests();
                  Console.WriteLine("about to dispose the client");
                  client.Dispose();
              }
          }
      }
  }
  </code></pre>
  
  <p>The code correctly cancels the request after 5 seconds but IIS still seem to send back data after the API call completes (set to 10 seconds).</p>
  
  <p>How can I close the connection so that IIS cannot send back any data after my 5s timeout?</p>