Ian Warburton
Ian Warburton

Reputation: 15676

Is it possible to cancel a specific request?

HttpClient has the method... CancelPendingRequests, which will,

Cancel all pending requests on this instance.

I'm using one HttpClient instance for my app, so is it possible to cancel one, specific request and leave any others to continue?

Upvotes: 1

Views: 95

Answers (1)

kuskmen
kuskmen

Reputation: 3775

You can cancel it by passing CancellationToken to the async client operation and tell it when to timeout, but keep in mind this does not guarantee that it will be canceled for sure - https://stackoverflow.com/a/36587037/3394884

var request = await _httpClient.GetAsync(new Uri("http://www.google.com"), 
                     new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);

Upvotes: 1

Related Questions