Hello,
Welcome to Microsoft Q&A!
How to cancel or stop a request initiated from UWP
For cancel or stop a request, you could refer to Customize request headers document. please check the custom DelegatingHandler
, it has CancellationToken
, you just make a property to get this token, and if you want to cancel or stop a request, just call cancel method.
public class MyHandler : DelegatingHandler
{
public CancellationTokenSource currentSource;
protected override async Task<HttpResponseMessage>
SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
currentSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
// Change the request-side here based on the HttpRequestMessage
request.Headers.Add("x-my-header", "my value");
// Do the request
var response = await base.SendAsync(request, cancellationToken);
// Change the response-side here based on the HttpResponseMessage
// Return the modified response
return response;
}
}
Usage
var handel = new MyHandler();
// cancel if necessary.
handel.currentSource?.Cancel();
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.