本文整理汇总了C#中System.Net.Http.HttpContent.ReadAsByteArrayAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContent.ReadAsByteArrayAsync方法的具体用法?C# HttpContent.ReadAsByteArrayAsync怎么用?C# HttpContent.ReadAsByteArrayAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpContent
的用法示例。
在下文中一共展示了HttpContent.ReadAsByteArrayAsync方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Write
public virtual void Write(JsonWriter writer, HttpContent content)
{
if (content == null)
return;
var bytes = content.ReadAsByteArrayAsync().ConfigureAwait(false).GetAwaiter().GetResult();
IEnumerable<string> contentTypeValues;
string contentTypeHeaderValue;
if (content.Headers.TryGetValues("Content-Type", out contentTypeValues))
contentTypeHeaderValue = contentTypeValues.Last();
else
contentTypeHeaderValue = "text/html; charset=utf-8";
writer.WritePropertyName("format");
var contentType = new ContentType(contentTypeHeaderValue);
bool formatAsBinary = false;
var encoding = Encoding.ASCII;
if (contentType.CharSet != null)
encoding = Encoding.GetEncoding(contentType.CharSet);
else
formatAsBinary = bytes.Any(c => c == 0 || c > 127);
if (formatAsBinary)
{
writer.WriteValue("binary");
writer.WritePropertyName("body");
writer.WriteValue(Convert.ToBase64String(bytes));
}
else
{
// Need to use memory stream to avoid UTF-8 BOM weirdness
string str;
using (var ms = new MemoryStream(bytes))
{
using (var sr = new StreamReader(ms, encoding))
{
str = sr.ReadToEnd();
}
}
if (contentType.MediaType == "application/json" || contentType.MediaType.EndsWith("+json"))
{
var jtoken = JToken.Parse(str);
writer.WriteValue("json");
writer.WritePropertyName("body");
jtoken.WriteTo(writer);
}
else
{
writer.WriteValue("text");
writer.WritePropertyName("body");
//writer.WriteValue(str);
// Represent the text as an array of strings split at \r\n to make long text content easier to read.
WriteStringFomat(writer, contentType, str);
}
}
}
示例2: ComputeHash
public byte[] ComputeHash(HttpContent httpContent)
{
using (var md5 = MD5.Create())
{
var content = httpContent.ReadAsByteArrayAsync().Result;
var hash = md5.ComputeHash(content);
return hash;
}
}
示例3: SetContent
/// <inheritdoc />
/// <exception cref="System.ArgumentNullException">
/// The <paramref name="content"/> parameter is <c>null</c>.
/// </exception>
protected internal override void SetContent(HttpContent content)
{
if (content == null)
{
throw new ArgumentNullException("content");
}
_content = content.ReadAsByteArrayAsync().Result;
}
示例4: ComputeHash
public static async Task<byte[]> ComputeHash(HttpContent httpContent)
{
using (var md5 = MD5.Create())
{
var content = await httpContent.ReadAsByteArrayAsync();
byte[] hash = md5.ComputeHash(content);
return hash;
}
}
示例5: ExtractBody
private static string ExtractBody(HttpContent content)
{
if (content == null)
{
return string.Empty;
}
var task = content.ReadAsByteArrayAsync();
return Encoding.UTF8.GetString(task.Result);
}
示例6: ConvertFromHttpContentAsync
/// <inheritdoc />
public async Task<object> ConvertFromHttpContentAsync(Type resultType, HttpContent httpContent, CancellationToken cancellationToken = default(CancellationToken))
{
if (!CanConvertFromHttpContent(resultType, httpContent))
{
throw new NotSupportedException("CanConvertFromHttpContent resulted in false, this is not supposed to be called.");
}
Log.Debug().WriteLine("Retrieving the content as byte[], Content-Type: {0}", httpContent.Headers.ContentType);
return await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false);
}
示例7: ComputeContentHashAsync
public static async Task<string> ComputeContentHashAsync(HttpContent httpContent)
{
if (httpContent == null)
return null;
using (SHA256 sha256 = SHA256.Create())
{
byte[] content = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false);
return content.Length != 0
? Convert.ToBase64String(sha256.ComputeHash(content))
: null;
}
}
示例8: CreateWrapperAsync
public static async Task<PreventDisposeContentWrapper> CreateWrapperAsync(HttpContent wrappedContent)
{
if (wrappedContent == null)
{
return new PreventDisposeContentWrapper(new byte[0], null);
}
var bytes = await wrappedContent.ReadAsByteArrayAsync();
var wrapper = new PreventDisposeContentWrapper(bytes, wrappedContent.GetType());
foreach (var header in wrappedContent.Headers)
{
wrapper.Headers.Add(header.Key, header.Value);
}
return wrapper;
}
示例9: DecompressContent
/// <summary>
/// Decompresses the compressed HTTP content.
/// </summary>
/// <param name="compressedContent">The compressed HTTP content.</param>
/// <param name="compressor">The compressor.</param>
/// <returns>The decompressed content.</returns>
public async Task<HttpContent> DecompressContent(HttpContent compressedContent, ICompressor compressor)
{
var decompressedContentStream = new MemoryStream();
// Decompress buffered content
using (var ms = new MemoryStream(await compressedContent.ReadAsByteArrayAsync()))
{
await compressor.Decompress(ms, decompressedContentStream).ConfigureAwait(false);
}
// Set position back to 0 so it can be read again
decompressedContentStream.Position = 0;
var decompressedContent = new StreamContent(decompressedContentStream);
// Copy content headers so we know what got sent back
this.CopyHeaders(compressedContent, decompressedContent);
return decompressedContent;
}
开发者ID:iNIC,项目名称:Microsoft.AspNet.WebApi.MessageHandlers.Compression,代码行数:26,代码来源:HttpContentOperations.cs
示例10: CreateBufferedCopyOfContent
internal static HttpContent CreateBufferedCopyOfContent(HttpContent content)
{
if (content != null)
{
SharedByteArrayContent shareableContent = content as SharedByteArrayContent;
byte[] contentBytes = shareableContent == null ?
content.ReadAsByteArrayAsync().Result :
shareableContent.ContentBytes;
HttpContent bufferedContent = new SharedByteArrayContent(contentBytes);
foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
{
bufferedContent.Headers.AddHeaderWithoutValidation(header);
}
return bufferedContent;
}
return null;
}
示例11: AssertContentLengthHeaderValue
private static void AssertContentLengthHeaderValue(HttpContent content)
{
long contentLength = content.ReadAsByteArrayAsync().Result.LongLength;
long contentLengthHeaderValue = content.Headers.ContentLength.GetValueOrDefault();
Assert.Equal(contentLength, contentLengthHeaderValue);
}
示例12: ComputeHash
private static async Task<byte[]> ComputeHash(HttpContent httpContent)
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = null;
var content = await httpContent.ReadAsByteArrayAsync();
if (content.Length != 0)
{
hash = md5.ComputeHash(content);
}
return hash;
}
}
示例13: CloneArrayAsync
/// <summary>
/// Creates an array copy of the <paramref name="content"/> without affecting the <see cref="Stream.Position"/> of the original object.
/// </summary>
private static async Task<byte[]> CloneArrayAsync(HttpContent content)
{
var result = await content.ReadAsByteArrayAsync();
var stream = await content.ReadAsStreamAsync();
if (stream.CanSeek) stream.Position = 0;
return result;
}