是否可以通过Application_BeginRequest获取Request Body?

时间:2014-09-19 20:53:43

标签: c# asp.net asp.net-mvc-4

正如标题所述;我正在研究这个,但没有找到任何关于检索POST的完整请求主体的细节。任何想法,如果这是可能的,我需要它填充IIS的服务器变量。

我有以下代码:

private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

            MethodInfo addStatic = null;
            MethodInfo makeReadOnly = null;
            MethodInfo makeReadWrite = null;

            Type type = application.Request.ServerVariables.GetType();
            MethodInfo[] methods = type.GetMethods(temp);

            foreach (MethodInfo method in methods)
            {
                switch (method.Name)
                {
                    case "MakeReadWrite": 
                        makeReadWrite = method;
                        break;
                    case "MakeReadOnly": 
                        makeReadOnly = method;
                        break;
                    case "AddStatic": 
                        addStatic = method;
                        break;
                }
            }

            makeReadWrite.Invoke(application.Request.ServerVariables, null);

            //GOAL IS TO RETRIEVE REQUEST BODY TO POPULATE SERVER VARIABLE, THEN DISPLAY VIA IIS.
            //MY CURRENT USE OF application.Request.Form DOESN'T WORK OF COURSE.
            string[] values = { "RequestFormParams", application.Request.Form.ToString() };

            addStatic.Invoke(application.Request.ServerVariables, values);
            makeReadOnly.Invoke(application.Request.ServerVariables, null);
        }

0 个答案:

没有答案