Windows Store app 8.1 Xaml pausing/resuming download using http classes

℡╲_俬逩灬. 提交于 2019-12-14 03:03:37

问题


I'm trying to use http classes to pause/resume a download and pausing is easy by using the cancellation token , however how can i resume a download?

I have used the background downloader class and it had a lot of issues in resuming and had many other issues to handle downloads.

Here is my code:

 using Windows.Web.Http;
 using Windows.Web.Http.Filters;

private async void Foo(StorageFolder folder, string fileName)
{

Uri uri = new Uri("http://localhost");
var filter = new HttpBaseProtocolFilter();
filter.ServerCredential =
    new Windows.Security.Credentials.PasswordCredential(uri.ToString(), "foo", "bar");
var client = new HttpClient(filter);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Headers.Add("Range", "bytes=0-");

// Hook up progress handler.
Progress<HttpProgress> progressCallback = new Progress<HttpProgress>(OnSendRequestProgress);
var tokenSource = new CancellationTokenSource();
HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(tokenSource.Token, progressCallback);

IInputStream inputStream = await response.Content.ReadAsInputStreamAsync();
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);

// Copy from stream to stream.
IOutputStream outputStream = await file.OpenAsync(FileAccessMode.ReadWrite);
await RandomAccessStream.CopyAndCloseAsync(inputStream, outputStream);
}

private void OnSendRequestProgress(HttpProgress obj)
{
 Debug.WriteLine(obj);
 }

来源:https://stackoverflow.com/questions/23977647/windows-store-app-8-1-xaml-pausing-resuming-download-using-http-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!