测试代码:

 每日踩坑 2018-09-29 .Net Core 控制器中读取 Request.Body

 

结果:

 

每日踩坑 2018-09-29 .Net Core 控制器中读取 Request.Body

 

PostMan:

每日踩坑 2018-09-29 .Net Core 控制器中读取 Request.Body

 

 

代码:

 1         private string GetRequestBodyUTF8String()
 2         {
 3             this.Request.EnableBuffering();
 4             this.Request.Body.Position = 0;
 5             Encoding encoding = System.Text.UTF8Encoding.Default;
 6             if (this.Request.ContentLength > 0 && this.Request.Body != null && this.Request.Body.CanRead)
 7             {
 8                 using (var buffer = new MemoryStream())
 9                 {
10                     this.Request.Body.CopyTo(buffer);
11                     buffer.Position = 0;
12                     var reader = new StreamReader(buffer, encoding);
13                     var body = reader.ReadToEnd();
14                     return body;
15                 }
16             }
17 
18             return string.Empty;
19         }

 

相关文章:

  • 2021-12-08
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
  • 2021-07-14
猜你喜欢
  • 2021-04-16
  • 2022-12-23
  • 2022-01-10
  • 2021-09-19
  • 2021-07-31
  • 2021-09-03
  • 2022-12-23
相关资源
相似解决方案