本文整理汇总了C#中System.Net.Http.HttpContent.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContent.CopyToAsync方法的具体用法?C# HttpContent.CopyToAsync怎么用?C# HttpContent.CopyToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpContent
的用法示例。
在下文中一共展示了HttpContent.CopyToAsync方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertStream
private Task ConvertStream(HttpContent httpContent, Stream outputStream)
{
Task convertTask = new Task(() => {
var convertSettings = new ConvertSettings {
CustomOutputArgs = "-map 0",
CustomInputArgs = "-vcodec h264"
};
var ffMpeg = new FFMpegConverter();
ffMpeg.ConvertProgress += FfMpeg_ConvertProgress;
ffMpeg.LogReceived += FfMpeg_LogReceived;
//var task = ffMpeg.ConvertLiveMedia(Format.h264, "C:\\Work\\Test\\converted.avi", Format.avi, convertSettings);
var task = ffMpeg.ConvertLiveMedia(Format.h264, outputStream, Format.mpeg, convertSettings);
task.Start();
var ffmpegStream = new FFMPegStream(task);
var copyTask = httpContent.CopyToAsync(ffmpegStream);
copyTask.Wait();
ffmpegStream.Close();
task.Wait();
// ffMpeg.ConvertMedia(@"C:\Work\Test\MyHomeSecureNode\devices\test\video.h264", "C:\\Work\\Test\\converted.avi", Format.avi);
outputStream.Close();
});
convertTask.Start();
return convertTask;
}
示例2: ConvertToStreamContentAsync
private async Task<StreamContent> ConvertToStreamContentAsync(HttpContent originalContent)
{
if (originalContent == null)
{
return null;
}
StreamContent streamContent = originalContent as StreamContent;
if (streamContent != null)
{
return streamContent;
}
MemoryStream ms = new MemoryStream();
await originalContent.CopyToAsync(ms);
// Reset the stream position back to 0 as in the previous CopyToAsync() call,
// a formatter for example, could have made the position to be at the end
ms.Position = 0;
streamContent = new StreamContent(ms);
// copy headers from the original content
foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
{
streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return streamContent;
}
示例3: ConvertToStreamContent
private StreamContent ConvertToStreamContent(HttpContent originalContent)
{
if (originalContent == null)
{
return null;
}
StreamContent streamContent = originalContent as StreamContent;
if (streamContent != null)
{
return streamContent;
}
MemoryStream ms = new MemoryStream();
originalContent.CopyToAsync(ms).Wait();
ms.Position = 0;
streamContent = new StreamContent(ms);
foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
{
streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return streamContent;
}
示例4: ConvertToStreamContent
private StreamContent ConvertToStreamContent(HttpContent originalContent)
{
if (originalContent == null)
{
return null;
}
var streamContent = originalContent as StreamContent;
if (streamContent != null)
{
return streamContent;
}
var ms = new MemoryStream();
// **** NOTE: ideally you should NOT be doing calling Wait() as its going to block this thread ****
// if the original content is an ObjectContent, then this particular CopyToAsync() call would cause the MediaTypeFormatters to
// take part in Serialization of the ObjectContent and the result of this serialization is stored in the provided target memory stream.
originalContent.CopyToAsync(ms).Wait();
ms.Position = 0;
streamContent = new StreamContent(ms);
// copy headers from the original content
foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
{
streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return streamContent;
}
示例5: ComputeHash
private static byte[] ComputeHash(HttpContent httpContent)
{
using (var md5 = MD5.Create())
{
byte[] hash = null;
if (httpContent != null)
{
var ms = new MemoryStream();
httpContent.CopyToAsync(ms).Wait();
ms.Seek(0, SeekOrigin.Begin);
var content = ms.ToArray();
if (content.Length != 0)
{
hash = md5.ComputeHash(content);
}
}
return hash;
}
}
示例6: WriteBufferedResponseContentAsync
internal static async Task WriteBufferedResponseContentAsync(HttpContextBase httpContextBase, HttpContent responseContent, HttpRequestMessage request)
{
Contract.Assert(httpContextBase != null);
Contract.Assert(responseContent != null);
Contract.Assert(request != null);
HttpResponseBase httpResponseBase = httpContextBase.Response;
// Return a task that writes the response body asynchronously.
// We guarantee we will handle all error responses internally
// and always return a non-faulted task.
Exception exception = null;
try
{
// Copy the HttpContent into the output stream asynchronously.
await responseContent.CopyToAsync(httpResponseBase.OutputStream);
}
catch (Exception e)
{
// Can't use await inside a catch block
exception = e;
}
if (exception != null)
{
// If we were using a buffered stream, we can still set the headers and status
// code, and we can create an error response with the exception.
// We create a continuation task to write an error response that will run after
// returning from this Catch() but before other continuations the caller appends to this task.
// The error response writing task handles errors internally and will not show as faulted.
await CreateErrorResponseAsync(httpContextBase, responseContent, request, exception);
}
}
示例7: WriteStreamedResponseContentAsync
internal static async Task WriteStreamedResponseContentAsync(HttpResponseBase httpResponseBase, HttpContent responseContent)
{
Contract.Assert(httpResponseBase != null);
Contract.Assert(responseContent != null);
try
{
// Copy the HttpContent into the output stream asynchronously.
await responseContent.CopyToAsync(httpResponseBase.OutputStream);
}
catch
{
// Streamed content may have been written and cannot be recalled.
// Our only choice is to abort the connection.
AbortConnection(httpResponseBase);
}
}
示例8: ReadAsFileAsync
/// <summary>
/// The read as file async.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="filename">The filename.</param>
/// <param name="overwrite">The overwrite.</param>
/// <returns>
/// The <see cref="Task" />.
/// </returns>
/// <remarks>Taken from <see href="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx" />.</remarks>
private async Task ReadAsFileAsync(HttpContent content, string filename, bool overwrite)
{
var pathname = Path.GetFullPath(filename);
if (!overwrite && File.Exists(filename))
{
throw new InvalidOperationException(string.Format("File {0} already exists.", pathname));
}
FileStream fileStream = null;
try
{
fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None);
await content.CopyToAsync(fileStream).ContinueWith((copyTask) => { fileStream.Close(); });
}
catch
{
if (fileStream != null)
{
fileStream.Close();
}
throw;
}
}
示例9: WriteBufferedResponseContentAsync
internal static Task WriteBufferedResponseContentAsync(HttpContextBase httpContextBase, HttpContent responseContent, HttpRequestMessage request)
{
Contract.Assert(httpContextBase != null);
Contract.Assert(responseContent != null);
Contract.Assert(request != null);
HttpResponseBase httpResponseBase = httpContextBase.Response;
Task writeResponseContentTask = null;
try
{
// Copy the HttpContent into the output stream asynchronously.
writeResponseContentTask = responseContent.CopyToAsync(httpResponseBase.OutputStream);
}
catch (Exception ex)
{
// Immediate exception requires an error response.
// Create a faulted task to share the code below
writeResponseContentTask = TaskHelpers.FromError(ex);
}
// Return a task that writes the response body asynchronously.
// We guarantee we will handle all error responses internally
// and always return a non-faulted task.
return writeResponseContentTask
.Catch((info) =>
{
// If we were using a buffered stream, we can still set the headers and status
// code, and we can create an error response with the exception.
// We create a continuation task to write an error response that will run after
// returning from this Catch() but before other continuations the caller appends to this task.
// The error response writing task handles errors internally and will not show as faulted.
Task writeErrorResponseTask = CreateErrorResponseAsync(httpContextBase, responseContent, request, info.Exception);
return info.Task(writeErrorResponseTask);
});
}
示例10: WriteStreamedResponseContentAsync
internal static Task WriteStreamedResponseContentAsync(HttpResponseBase httpResponseBase, HttpContent responseContent)
{
Contract.Assert(httpResponseBase != null);
Contract.Assert(responseContent != null);
Task writeResponseContentTask = null;
try
{
// Copy the HttpContent into the output stream asynchronously.
writeResponseContentTask = responseContent.CopyToAsync(httpResponseBase.OutputStream);
}
catch
{
// Streamed content may have been written and cannot be recalled.
// Our only choice is to abort the connection.
AbortConnection(httpResponseBase);
return TaskHelpers.Completed();
}
return writeResponseContentTask
.Catch((info) =>
{
// Streamed content may have been written and cannot be recalled.
// Our only choice is to abort the connection.
AbortConnection(httpResponseBase);
return info.Handled();
});
}