Windows 8.1 中的 http 客户端取消请求 [英] http client cancel request in windows 8.1

查看:79
本文介绍了Windows 8.1 中的 http 客户端取消请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Windows Phone 8.1 项目.Windows 8.1 中有两个版本的 http 客户端 - system.net.httpwindows.web.http.Microsoft 建议使用后者.

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 recommends using the later.

所以,我决定去.但是我找不到取消以 windows.web.http.httpclient 开头的 Web 请求的方法.在 system.net.http.httpclient 中有一个 CancelPendingRequests 方法,但后面的选项没有类似的方法.

So, I decided to go with it. But I can't find a way to cancel the web request started with windows.web.http.httpclient. In system.net.http.httpclient there is a CancelPendingRequests method but no similar methods exist for the later option.

那么,是否可以取消 Web 请求,如果可以,如何取消?

So, is it possible to cancel the web request and if so how?

示例代码:

考虑一个 Http Get 请求谷歌如下.如果用户愿意,我想在它完成之前取消它.

Consider a Http Get request google as follows. I would like to cancel it before its completion if the user wishes so.

// Windows Phone 8.1 project (not silverlight)
public sealed partial class MainPage : Page
{
    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;

        Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, e) =>
        {
           CancelGet();
        }
    }

    private void CancelGet()
    {
       // What to put here??
       // tried client.Dispose();
       // but still get request completes successfully
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        string res = await client.GetStringAsync(new Uri("http://www.google.com", UriKind.Absolute));

        System.Diagnostics.Debug.WriteLine("result obtained\n" + res);
    }
 }

HttpClient 的所有方法都不支持 CancellationToken.

None of the methods of HttpClient support CancellationToken.

我还尝试在请求正在进行时在 HttpClient 上调用 Dispose()(比如 GetStringAsync).但是仍然没有抛出任何异常,也没有取消请求;GetStringAsync 正常完成并显示正确的结果.

Also I tried to call Dispose() on the HttpClient while the request is ongoing (say GetStringAsync). However still neither any exception is thrown nor the request is cancelled; the GetStringAsync completes as normal and shows the correct result.

推荐答案

还没有测试过,但这可以工作:

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

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

如果你不需要取消令牌,你也可以像这样直接取消IAsyncOperation:

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();

这篇博文总体上是一本不错的读物任务与 IAsyncOperation 主题.

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

这篇关于Windows 8.1 中的 http 客户端取消请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆