How to get the Download/Upload status(in %) from Http Client in Windows 8.1 Universal App?

喜夏-厌秋 提交于 2019-12-08 05:57:24

问题


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

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

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.


回答1:


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
        {
        }

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.



来源:https://stackoverflow.com/questions/30341536/how-to-get-the-download-upload-statusin-from-http-client-in-windows-8-1-univ

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