是否可以在
Windows 8中取消HttpClient GET Web请求.我正在寻找一种解决方案来取消我的Web请求,如果用户按下页面上的键.在我的应用程序中,我使用静态类来创建Web请求.
Alos我在应用程序中使用MVVM Light和静态视图模型.
在目前的情况下,即使用户按下后退按钮,
vm保持活动状态,回叫到达并在VM中执行.
所以我正在寻找一个解决方案来取消背面的请求.
试试这个
原文链接:https://www.f2er.com/windows/364545.htmlprotected async override void OnNavigatedTo(NavigationEventArgs e) { await HttpGetRequest(); } public CancellationTokenSource cts = new CancellationTokenSource(); private async Task HttpGetRequest() { try { DateTime now = DateTime.Now; var httpClient = new HttpClient(); var message = new HttpRequestMessage(HttpMethod.Get,"https://itunes.apple.com/us/RSS/toppaidapplications/limit=400/genre=6000/json"); var response = await httpClient.SendAsync(message,cts.Token); System.Diagnostics.Debug.WriteLine("HTTP Get request completed. Time taken : " + (DateTime.Now - now).TotalSeconds + " seconds."); } catch (TaskCanceledException) { System.Diagnostics.Debug.WriteLine("HTTP Get request canceled."); } } private void btnCancel_Click(object sender,RoutedEventArgs e) { cts.Cancel(); }