Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1.  internal static class Extensions
  2.     {
  3.         internal static async Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request, CancellationToken ct)
  4.         {
  5.             using (ct.Register(() => request.Abort(), useSynchronizationContext: false))
  6.             {
  7.                 WebResponse response;
  8.                 try
  9.                 {
  10.                     response = await request.GetResponseAsync().ConfigureAwait(false);
  11.                     return (HttpWebResponse)response;
  12.                 }
  13.                 catch (Exception ex)
  14.                 {
  15.                     // WebException is thrown when request.Abort() is called,
  16.                     // but there may be many other reasons,
  17.                     // propagate the WebException to the caller correctly
  18.  
  19.                     if (ct.IsCancellationRequested)
  20.                     {
  21.                         // the WebException will be available as Exception.InnerException
  22.                         throw new OperationCanceledException(ex.Message, ex, ct);
  23.                     }
  24.  
  25.                     // cancellation hasn't been requested, rethrow the original WebException
  26.                     throw;
  27.                 }
  28.             }
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement