コード例 #1
0
        /// <summary>
        /// 添加表单参数
        /// </summary>
        /// <param name="request">Http请求</param>
        /// <param name="log">日志</param>
        private async Task AddFormParamsAsync(Microsoft.AspNetCore.Http.HttpRequest request, ILog log)
        {
            if (IsMultipart(request.ContentType))
            {
                return;
            }
            request.EnableBuffering();
            var result = await FileHelper.ToStringAsync(request.Body, isCloseStream : false);

            if (string.IsNullOrWhiteSpace(result))
            {
                return;
            }
            log.Params("表单参数:").Params(result);
        }
コード例 #2
0
        public static async Task <string> ReadBodyStream(this HttpRequest httpRequest)
        {
            httpRequest.EnableBuffering();

            var reader = new StreamReader(httpRequest.Body);

            var body = await reader.ReadToEndAsync();

            if (httpRequest.Body.CanSeek)
            {
                httpRequest.Body.Seek(0, SeekOrigin.Begin);
            }

            return(body);
        }
コード例 #3
0
        // https://www.programmersought.com/article/49936898629/
        public static async Task <string> GetRequestBodyAsync(this HttpRequest request)
        {
            FulcrumAssert.IsNotNull(request.Body, CodeLocation.AsString());
            request.EnableBuffering();
            request.Body.Position = 0;
            var sr   = new StreamReader(request.Body);
            var body = await sr.ReadToEndAsync();

            request.Body.Position = 0;
            if (body == "")
            {
                FulcrumAssert.IsTrue(request.ContentLength == null || request.ContentLength.Value == 0, CodeLocation.AsString());
            }
            return(body);
        }
コード例 #4
0
        public static async Task <string> ReadBodyStream(this HttpRequest httpRequest)
        {
            httpRequest.EnableBuffering();

            var encoding = GetEncodingFromContentType(httpRequest.ContentType) ?? Encoding.UTF8;
            var reader   = new StreamReader(httpRequest.Body, encoding);

            var body = await reader.ReadToEndAsync();

            if (httpRequest.Body.CanSeek)
            {
                httpRequest.Body.Seek(0, SeekOrigin.Begin);
            }

            return(body);
        }
コード例 #5
0
        /// <summary>
        /// 获取请求日志
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static async Task <string> RequestStringAsync(this HttpRequest request)
        {
            var message = new StringBuilder();

            if (request.Method != null)
            {
                message.AppendLine($"[Method]: {request.Method} ");
            }
            message.AppendLine($"[RequestUri]: {request.GetAbsoluteUri()} ");

            //头部Header
            if (request.Headers != null)
            {
                message.AppendLine("[Header] Values: ");
                foreach (var headerItem in request.Headers)
                {
                    message.AppendLine($"--> [{headerItem.Key}]: {headerItem.Value} ");
                }
            }
            //Body
            if (!string.IsNullOrWhiteSpace(request.Method) && !request.Method.ToUpper().Equals("GET") && !request.Headers["Content-Type"].ToString().ToLower().Equals("multipart/form-data"))
            {
                request.EnableBuffering();

                await request.Body.DrainAsync(CancellationToken.None);

                request.Body.Seek(0L, SeekOrigin.Begin);

                var bodyContent = string.Empty;

                await using (var ms = new MemoryStream(2048))
                {
                    request.Body.Position = 0;
                    request.Body.CopyTo(ms);
                    var content = ms.ToArray();
                    bodyContent = Encoding.UTF8.GetString(content);
                }

                message.AppendLine("[Body]: ");
                message.AppendLine($"--> {bodyContent}");

                request.Body.Seek(0L, SeekOrigin.Begin);
            }

            return(message.ToString());
        }
コード例 #6
0
        private async Task <string> FormatRequest(Microsoft.AspNetCore.Http.HttpRequest request)
        {
            request.EnableBuffering();

            var body = request.Body;

            var buffer = new byte[Convert.ToInt32(request.ContentLength)];

            await request.Body.ReadAsync(buffer, 0, buffer.Length);

            var bodyAsText = Encoding.UTF8.GetString(buffer);

            body.Seek(0, SeekOrigin.Begin);

            request.Body = body;

            return($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}");
        }