http client cancel request in windows 8.1

后端 未结 1 1090
失恋的感觉
失恋的感觉 2021-02-03 14:21

I am working on a Windows Phone 8.1 project. There are two versions of http client in Windows 8.1 - system.net.http and windows.web.http. Microsoft re

相关标签:
1条回答
  • 2021-02-03 14:46

    Haven't tested it yet, but this could work:

    await client.GetStringAsync(new Uri("http://www.google.com")).AsTask(cancellationToken);
    

    If you don't have the need for cancellation tokens, you can also cancel the IAsyncOperation directly like this:

    var operation = _httpClient.GetStringAsync(new Uri("http://www.google.com"));
    var response = await operation;
    
    operation.Cancel();
    

    This blog post is a good read on the whole Task vs IAsyncOperation topic.

    0 讨论(0)
提交回复
热议问题