Asp.NetCore3.1中多次读取Request.Body

        // POST: Reg
        [HttpPost]
        public async Task<string> Post()
        {

            //StreamReader sr = new StreamReader(Request.Body);
            //string data = await sr.ReadToEndAsync();

            string data = "";
            using (MemoryStream ms=new MemoryStream())
            {
                await Request.Body.CopyToAsync(ms);
                //设置当前流的位置为0
                ms.Seek(0, SeekOrigin.Begin);
                logger.LogInformation("ms.Length=" + ms.Length);
                //这里ReadToEnd执行完毕后requestBodyStream流的位置会从0到最后位置(即request.ContentLength)
                data = new StreamReader(ms, Encoding.UTF8).ReadToEnd();
                logger.LogInformation("data=" + data);

                //设置当前流的位置为0
                ms.Seek(0, SeekOrigin.Begin);
                Request.Body = ms;
                StreamReader sr = new StreamReader(Request.Body);
                string data2 = await sr.ReadToEndAsync();
                logger.LogInformation("data2=" + data2);
            }

            string header = $"请求头:\r\n";
            foreach (var item in Request.Headers)
            {
                header += $"{item.Key}:{item.Value}\r\n";
            }
            logger.LogInformation(header);

            var ip = Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                //ip = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                //ip = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
                ip = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            }
            logger.LogInformation("ip=" + ip);

            //...
        }

 

你可能感兴趣的:(Asp.NetCore3.1中多次读取Request.Body)