How to get raw request body in ASP.NET?

前端 未结 6 1159
你的背包
你的背包 2021-01-07 16:12

In the HttpApplication.BeginRequest event, how can I read the entire raw request body? When I try to read it the InputStream is of 0 length, leadin

6条回答
  •  没有蜡笔的小新
    2021-01-07 16:59

    It is important to reset position of InputStream.

    var memstream = new MemoryStream();
    Request.InputStream.CopyTo(memstream);
    Request.InputStream.Position = 0;
    using (StreamReader reader = new StreamReader(memstream)) {
        var text = reader.ReadToEnd();
        Debug.WriteLine(text);
    }
    

提交回复
热议问题