Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static class Extensions
- {
- internal static async Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request, CancellationToken ct)
- {
- using (ct.Register(() => request.Abort(), useSynchronizationContext: false))
- {
- WebResponse response;
- try
- {
- response = await request.GetResponseAsync().ConfigureAwait(false);
- return (HttpWebResponse)response;
- }
- catch (Exception ex)
- {
- // WebException is thrown when request.Abort() is called,
- // but there may be many other reasons,
- // propagate the WebException to the caller correctly
- if (ct.IsCancellationRequested)
- {
- // the WebException will be available as Exception.InnerException
- throw new OperationCanceledException(ex.Message, ex, ct);
- }
- // cancellation hasn't been requested, rethrow the original WebException
- throw;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement