本文整理汇总了C#中System.Net.Http.HttpClient.PostAsyncCancellationSafe方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.PostAsyncCancellationSafe方法的具体用法?C# HttpClient.PostAsyncCancellationSafe怎么用?C# HttpClient.PostAsyncCancellationSafe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.PostAsyncCancellationSafe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NewFileAsync
//.........这里部分代码省略.........
byte[] bytes = new byte[chunkSizes[chunkIndex]];
using (await SemaphoreLock.TakeAsync(concurrentUploadSemaphore))
{
chunkUploadsCancellationSource.Token.ThrowIfCancellationRequested();
using (await SemaphoreLock.TakeAsync(concurrentReadSemaphore))
{
chunkUploadsCancellationSource.Token.ThrowIfCancellationRequested();
chunkFeedback.Status = "Reading contents";
// Read in the raw bytes for this chunk.
contents.Position = startOffset;
contents.Read(bytes, 0, bytes.Length);
}
chunkFeedback.Status = "Encrypting contents";
byte[] chunkMac;
Algorithms.EncryptNodeDataChunk(bytes, dataKey, nonce, out chunkMac, startOffset);
chunkMacs[chunkIndex] = chunkMac;
await RetryHelper.ExecuteWithRetryAsync(async delegate
{
chunkUploadsCancellationSource.Token.ThrowIfCancellationRequested();
chunkFeedback.Status = string.Format("Uploading {0} bytes", chunkSizes[chunkIndex]);
var url = beginUploadResult.UploadUrl + "/" + startOffset;
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsyncCancellationSafe(url, new ByteArrayContent(bytes), chunkUploadsCancellationSource.Token);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
// Result from last chunk is: base64-encoded completion handle to give to NewItemsCommand
// Negative ASCII integer in case of error. Standard-ish stuff?
// Empty is just OK but not last chunk.
if (responseBody.StartsWith("["))
{
// Error result!
// Assuming it is formatted like this, I never got it to return an error result.
// It always just hangs if I do anything funny...
var errorResult = JObject.Parse(responseBody);
Channel.ThrowOnFailureResult(errorResult);
throw new ProtocolViolationException("Got an unexpected result from chunk upload: " + responseBody);
}
else if (!string.IsNullOrWhiteSpace(responseBody))
{
// Completion token!
completionToken = responseBody;
}
if (bytes.Length != chunkSizes[chunkIndex])
throw new MegaException(string.Format("Expected {0} bytes in chunk but got {1}.", chunkSizes[chunkIndex], bytes.Length));
}, ChunkUploadRetryPolicy, chunkFeedback, chunkUploadsCancellationSource.Token);
}
Interlocked.Add(ref completedBytes, chunkSizes[chunkIndex]);
}