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


C# HttpContent.ReadAsByteArrayAsync方法代码示例

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

示例2: ComputeHash

 public byte[] ComputeHash(HttpContent httpContent)
 {
     using (var md5 = MD5.Create())
     {
         var content = httpContent.ReadAsByteArrayAsync().Result;
         var hash = md5.ComputeHash(content);
         return hash;
     }
 }
开发者ID:JoeDoyle23,项目名称:CSharpSnippets,代码行数:9,代码来源:MD5Helper.cs

示例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;
        }
开发者ID:roryprimrose,项目名称:Headless,代码行数:13,代码来源:BinaryPage.cs

示例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;
     }
 }
开发者ID:rbanks54,项目名称:HMAC.Sample,代码行数:9,代码来源:MD5Helper.cs

示例5: ExtractBody

        private static string ExtractBody(HttpContent content)
        {
            if (content == null)
            {
                return string.Empty;
            }

            var task = content.ReadAsByteArrayAsync();
            return Encoding.UTF8.GetString(task.Result);
        }
开发者ID:hpence,项目名称:ApiProxy,代码行数:10,代码来源:LoggingHandler.cs

示例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);
		}
开发者ID:dapplo,项目名称:Dapplo.HttpExtensions,代码行数:11,代码来源:ByteArrayHttpContentConverter.cs

示例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;
            }
        }
开发者ID:BinaryTENSHi,项目名称:async-test,代码行数:13,代码来源:AuthenticationHelper.cs

示例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;
 }
开发者ID:Xamarui,项目名称:acme.net,代码行数:14,代码来源:PreventDisposeContentWrapper.cs

示例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;
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:21,代码来源:HttpRequestMessageExtensionMethods.cs

示例11: AssertContentLengthHeaderValue

 private static void AssertContentLengthHeaderValue(HttpContent content)
 {
     long contentLength = content.ReadAsByteArrayAsync().Result.LongLength;
     long contentLengthHeaderValue = content.Headers.ContentLength.GetValueOrDefault();
     Assert.Equal(contentLength, contentLengthHeaderValue);
 }
开发者ID:RhysC,项目名称:aspnetwebstack,代码行数:6,代码来源:HttpContentMultipartExtensionsTests.cs

示例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;
     }
 }
开发者ID:mgalpy,项目名称:WebApiHMACAuthentication,代码行数:13,代码来源:HMACAuthenticationAttribute.cs

示例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;
 }
开发者ID:1and1,项目名称:TypedRest-DotNet,代码行数:10,代码来源:EtagEndpointBase.cs


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