当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.PostAsyncCancellationSafe方法代码示例

本文整理汇总了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]);
						}
开发者ID:AIBrain,项目名称:mega-client,代码行数:67,代码来源:CloudItem.cs


注:本文中的System.Net.Http.HttpClient.PostAsyncCancellationSafe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。