private async Task<string> HandleUploadAsync(UploadOperation upload, CancellationTokenSource cts)
{
string responseBody = string.Empty;
try
{
Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
UploadOperation operation;
if(cts == null)
{
cts = new CancellationTokenSource();
}
var ct = CommonUtil.GetCancellationToken(cts);
operation = newUpload ? await upload.StartAsync().AsTask(ct, progressCallback) : await upload.AttachAsync().AsTask(ct, progressCallback);
if(cts.IsCancellationRequested)
{
throw new NoInternetAccessException("No Internet Access");
}
else if(cts != null)
{
cts.Dispose();
}
using (var response = operation.GetResultStreamAt(0))
{
uint size = (uint)operation.Progress.BytesReceived;
IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
if (size > 0)
{
var f = await response.ReadAsync(buffer, size, InputStreamOptions.None);
using (var dr = DataReader.FromBuffer(f))
{
responseBody = dr.ReadString(dr.UnconsumedBufferLength);
}
}
}
return responseBody;
}
catch (TaskCanceledException)
{
throw new NoInternetException("No Internet Access");
}
catch (Exception ex)
{
throw ex;
}
}