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


首先,以下方式是錯誤的(這個重復讀取只能在using語句里面生效,因為用了ms對象賦值,當using結束后ms資源被釋放,Request.Body就無法再讀了)
        [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);

            //...
        }
正確方式是:
        [HttpPost]
        public async Task<string> Post()
        {
            //StreamReader sr = new StreamReader(Request.Body);
            //string data = await sr.ReadToEndAsync();

            string data = "";

            //Request.EnableBuffering();可以實現多次讀取Body
            Request.EnableBuffering();
            StreamReader sr = new StreamReader(Request.Body);
            data = await sr.ReadToEndAsync();
            logger.LogInformation("data=" + data);
            Request.Body.Seek(0, SeekOrigin.Begin);

            //再次讀取 依然可以成功讀到
            Request.EnableBuffering();
            StreamReader sr2 = new StreamReader(Request.Body);
            string data2 = await sr2.ReadToEndAsync();
            logger.LogInformation("data2=" + data2);
            Request.Body.Seek(0, SeekOrigin.Begin);
            

            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);

            //...
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM