title: C# HttpWebRequest - Stop waiting for response
body: <p>I am making a HEAD request to an external server to identify if my credentials are valid. However, the external server (that I don't have control over) is processing my request as if it's a GET (and starts generating a CSV to stream that can take up to 30 minutes to generate.)</p>
  <p>If my credentials are incorrect, I immediately get back a 401. If the request takes a long time (more than 10 seconds), I know my credentials are correct b/c the external server has started to generate the CSV.</p>
  <p>I want my request to timeout after 10 seconds. But I'm having issues w/ <code>HttpWebRequest.Timeout</code> and <code>HttpWebRequest.ReadWriteTimeout</code>... the values don't seem to be acknowledged &amp; my request will wait seemingly forever for the full response.</p>
  <p>How do I get the timeouts to actually happen?</p>
  <p>Here's what I've got:</p>
  <pre><code>public async Task&lt;bool&gt; GetResponse(string url, string username, string secret)
  {
      HttpWebRequest _request = new HttpWebRequest(url);
      NetworkCredential _credential = new NetworkCredential(username, secret);
      CredentialCache _credentialCache = new CredentialCache { { url, &quot;Basic&quot;, _credential } };
      _request.PreAuthenticate = true;
      _request.Credentials = _credentialCache;
  
      // I set all 3 of these timeouts while testing but none of them seem to be acknowledged.
      _request.ContinueTimeout = 10000;
      _request.ReadWriteTimeout = 10000;
      _request.Timeout = 10000;
  
      try
      {
          using (HttpWebResponse _response = await _request.GetResponseAsync())
          {
              if (_response.StatusCode == HttpStatusCode.OK &amp;&amp; _response.ContentType.Contains(&quot;text/csv&quot;)
              {
                  return true; // successfully retrieved response.
              }
  
              return false; // failed to retrieve successful response.
          }
      }
      catch (WebException ex)
      {
          if (ex.Status = WebExceptionStatus.Timeout)
          {
              return true; // timeout == success.
          }
  
          return false;
      }
  }
  
  </code></pre>