How to get raw request body in ASP.NET?

前端 未结 6 1185
你的背包
你的背包 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:47

    The request object is not populated in the BeginRequest event. You need to access this object later in the event life cycle, for example Init, Load, or PreRender. Also, you might want to copy the input stream to a memory stream, so you can use seek:

    protected void Page_Load(object sender, EventArgs e)
    {
        MemoryStream memstream = new MemoryStream();
        Request.InputStream.CopyTo(memstream);
        memstream.Position = 0;
        using (StreamReader reader = new StreamReader(memstream))
        {
            string text = reader.ReadToEnd();
        }
    }
    

提交回复
热议问题