问题
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