title: Abort GetResponseAsync() and Exception in Task
body: <p>I have written a method which get <code>ETag</code> from eq. <code>XML</code> file on server. Am I correct wrote abort task if timeout (<code>GetResponseAsync()</code> doesn't have <code>CancellationToken</code>) and I have no other idea how to do <code>Exception</code>.</p>
  
  <p><strong>Here's code:</strong></p>
  
  <pre><code>    public static async Task&lt;string&gt; GetETagAsync(Uri feedLink)
      {
          const int millisecondsTimeout = 2500;
          WebRequest webRequest = WebRequest.Create(feedLink);
          webRequest.Method = "HEAD";
          try
          {
              Task&lt;WebResponse&gt; webResponse = webRequest.GetResponseAsync();
              if (await Task.WhenAny(webResponse, Task.Delay(millisecondsTimeout)) == webResponse)
              {
                  using (var result = webResponse.Result)
                  {
                      return result.Headers["ETag"];
                  }
              }
              else
              {
                  webRequest.Abort();
                  return null;
              }
          }
          catch (Exception)
          {
              return null;
          }
      }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>I have made some changes. Rewrite exceptions and use class from this topic: <a href="https://stackoverflow.com/questions/19211972/getresponseasync-does-not-accept-cancellationtoken">GetResponseAsync does not accept cancellationToken</a></p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>    public static async Task&lt;string&gt; GetETagAsync(Uri feedLink)
      {
          const int millisecondsTimeout = 2500;
          var cancellationTokenSource = new CancellationTokenSource();
          WebRequest webRequest = WebRequest.Create(feedLink);
          webRequest.Method = "HEAD";
  
          try
          {
              Task&lt;WebResponse&gt; webResponse = WebRequestExtensions.GetResponseAsync(webRequest, cancellationTokenSource.Token);
              if (await Task.WhenAny(webResponse, Task.Delay(millisecondsTimeout)) == webResponse)
              {
                  using (var result = webResponse.Result)
                  {
                      return result.Headers["ETag"];
                  }
              }
              else
              {
                  cancellationTokenSource.Cancel();
                  return null;
              }
          }
          catch (AggregateException ex)
          {
              if (ex.InnerException is WebException)
                  return null;
  
              throw;
          }
      }
  
  public static class WebRequestExtensions
  {
      public static async Task&lt;WebResponse&gt; GetResponseAsync(this WebRequest request, CancellationToken cancellationToken)
      {
          using (cancellationToken.Register(() =&gt; request.Abort(), useSynchronizationContext: false))
          {
              try
              {
                  var response = await request.GetResponseAsync();
                  cancellationToken.ThrowIfCancellationRequested();
                  return (WebResponse)response;
              }
              catch (WebException ex)
              {
                  if (ex.Status == WebExceptionStatus.RequestCanceled)
                  {
                      cancellationToken.ThrowIfCancellationRequested();
                  }
  
                  if (cancellationToken.IsCancellationRequested)
                  {
                      throw new TaskCanceledException(ex.Message, ex);
                  }
  
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p>Is it correct now?</p>