zoukankan      html  css  js  c++  java
  • Asp.NetCore3.1 asp.net 5.0 中多次读取 Request.Body Ajax Post过来的JSON数据等 已验证有效

    首先,以下方式是错误的(这个重复读取只能在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 = $"请求头:
    ";
                foreach (var item in Request.Headers)
                {
                    header += $"{item.Key}:{item.Value}
    ";
                }
                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 = $"请求头:
    ";
                foreach (var item in Request.Headers)
                {
                    header += $"{item.Key}:{item.Value}
    ";
                }
                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);
    
                //...
            }
     
  • 相关阅读:
    easyui属性表格的一点小总结
    我是程序员:国庆带女朋友回家见父母
    我睁着朦胧呆滞的双眼立在初秋的夜里
    iframe父子兄弟之间调用传值(contentWindow && parent)
    向前兼容和向后兼容的含义
    char 与 String 相等比较
    2012国庆12天的长假
    JavaScript遍历XML总结
    Spring3.x企业应用开发实战Spring+Hibernat架构分析
    w3school上系统过了一遍Jquery的总结
  • 原文地址:https://www.cnblogs.com/webenh/p/13111476.html
Copyright © 2011-2022 走看看