zoukankan      html  css  js  c++  java
  • net core 3.0 读取post请求body方法

    读取报错了

    一 错误信息:Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronou

    二 解决方法

    1 在startup中设置同步读取方式,读取body内容。默认是异步

      services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
         .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
    

    2

    string body=string.Empty;
     int bufferThreshold = 5120000;//500k  bufferThreshold 设置的是 Request.Body 最大缓存字节数(默认是30K) 超出这个阈值的字节会被写入磁盘
                    int bufferLimit = 10240000;//1m 设置的是 Request.Body 允许的最大字节数(默认值是null),超出这个限制,就会抛出异常 System.IO.IOException
                    Request.EnableBuffering(bufferThreshold, bufferLimit);
                    HttpContext.Request.Body.Position = 0;
                    if (this.Request.ContentLength > 0 && this.Request.Body != null && this.Request.Body.CanRead)
                    {
                        Encoding encoding = System.Text.UTF8Encoding.Default;
                        using (var buffer = new MemoryStream())
                        {
                            HttpContext.Request.Body.CopyTo(buffer);
                            buffer.Flush();
                            buffer.Position = 0;
                            body = buffer.GetReader().ReadToEnd();
                        }
                    }
    

    body即为读取到的内容。
    如果是文件的话 保存方法为 buffer.ToFile(文件路径);
    Request.EnableBuffering 选项,使读取的内容能多次读取。默认读取一次就清理了。


    作者:过错
    出处:http://www.cnblogs.com/wang2650/
    关于作者:net开发做的久而已。十余年时光虚度!
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com  联系我,非常感谢。

  • 相关阅读:
    ArrayList用法
    MessageBox
    将文本文件导入Sql数据库
    在桌面和菜单中添加快捷方式
    泡沫排序
    Making use of localized variables in javascript.
    Remove double empty lines in Visual Studio 2012
    Using Operations Manager Connectors
    Clear SharePoint Designer cache
    Programmatically set navigation settings in SharePoint 2013
  • 原文地址:https://www.cnblogs.com/wang2650/p/14330315.html
Copyright © 2011-2022 走看看