如何从 Windows 8.1 通用应用程序中的 Http 客户端获取下载/上传状态(以 % 为单位)? [英] How to get the Download/Upload status(in %) from Http Client in Windows 8.1 Universal App?

查看:12
本文介绍了如何从 Windows 8.1 通用应用程序中的 Http 客户端获取下载/上传状态(以 % 为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个规范,其中需要忽略 HTTPS 证书并获取上传/下载的状态.

I have a spec in which there is a need to ignore HTTPS certificates and also to get the status of Upload/Download.

我正在使用 HttpClient 忽略证书,但我找不到获取下载/上传状态的方法.

I am using HttpClient to ignore the certificate but i could not find the way to get the status of downloading/Uploading.

我知道它在 WebClient Windows Phone 8 中存在,而 Webclient 在 Windows 8.1 中不存在.

I know it was there in WebClient Windows Phone 8 and Webclient not there in Windows 8.1.

所以请指导我完成这两件事.

So please guide me to accomplish both these things.

推荐答案

对于下载进度,您可以从您的 Http 请求返回响应流,并可以使用以下代码写入文件,同时显示进度状态

For Download progress, You can return response stream from your Http request and can use below code to write to file which also display progress status

 IInputStream inputStream = null;
            IRandomAccessStream fs = null;
            try
            {
                inputStream = responseStream.AsInputStream();
                ulong totalBytesRead = 0;
                fs = await file.OpenAsync(FileAccessMode.ReadWrite);
                ulong fileSize = Convert.ToUInt64(Size);

                while (true)
                {
                    // Read from the web.
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        IBuffer buffer = new Windows.Storage.Streams.Buffer(512);
                        buffer = await inputStream.ReadAsync(
                           buffer,
                           buffer.Capacity,
                           InputStreamOptions.None);

                        if (buffer.Length == 0)
                        {
                            // There is nothing else to read.
                            break;
                        }


                        // Report progress.

                        totalBytesRead += buffer.Length;

                        double progress = ((double)totalBytesRead / fileSize);
                        double Value = progress * 100;

                        System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead);

                        await fs.WriteAsync(buffer);

                    }
                    else
                    {
                        inputStream.Dispose();
                        fs.Dispose();

                        await file.DeleteAsync();
                        break;
                    }
                }

                inputStream.Dispose();
                fs.Dispose();


        }
        catch
        {
        }

对于上传进度,没有内置支持来做到这一点,但你可以使用ProgressMessageHandler 在 System.Net.Http.Handler 中找到,您可以在 Nuget 上找到它,然后将其挂钩到您的 HttpClient 对象.

For upload Progress, there is no in built support to do that, but you can use ProgressMessageHandler found in System.Net.Http.Handler which you can found on Nuget and than hook that to your HttpClient Object.

这篇关于如何从 Windows 8.1 通用应用程序中的 Http 客户端获取下载/上传状态(以 % 为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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