Is it possible to cancel a call to HttpClient.SendAsync()
?
I'm sending some data like this:
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "some url");
var multipartFormDataContent = new MultipartFormDataContent();
// ... construction of the MultipartFormDataContent. It contains form data + picture file
requestMessage.Content = multipartFormDataContent;
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
This code works perfectly, but I need to be able to cancel a request on user demand. Is this possible?
I see that there is an overload of SendAsync
that accepts a CancellationToken
but I don't know how to use it. I also know about a property called IsCancellationRequested
that indicates if a request has been canceled. But how do I go about actually canceling a request?