简体   繁体   中英

How to read data from HttpRequest body in asp.net web forms

I have created a POST request with some credential information on Request body.

The below request details, I have captured from fiddler.

在此处输入图片说明

Raw Data:

POST http://localhost/AutovhcReport/rdPage.aspx?&rdframeid=rdFrame3416d447-50ed-f635-ed09-cdcb201017ee HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 56
Cache-Control: max-age=0
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: ddllanguage=en-GB; ASP.NET_SessionId=j50rws53zw1wrf4pn0yqmeot; rdPanelExpanded_Table=True; rdTablePanelMenuExpanded=False; rdAllowRedo=False; rdAllowUndo=False

rdReport=SampleReport&rdembedded=true&rdUserName=Dynamic

How to read the body values in Asp.net web forms C# ?

I have tried this, LoginPage.aspx

<%@ Page Language="c#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections.Specialized" %>

<script language="c#" runat="server">

    public void Page_Load(object sender, EventArgs e)
    {
        string username= HttpContext.Current.Request.Form["rdUserName"].ToString();
        Session.Add("rdUserName",username);  
        Session.Add("SiteCode", "7");
        Session.Add("UserName", "xxx");
        Session.Add("Password", "xxxxx");
        HttpContext.Current.Response.Redirect("rdPage.aspx");

    }
</script>

In ASP.NET web forms there are objects that holds all the http data. So in the desired event handler just read values by key

string val = Request.Form["rdReport"]

or iterate array

Request.Form[]

Request.Form["key"] should work. Alternatively you can try Request.Form[index] Here is my sample code:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(Request.HttpMethod.ToString() + "<br/>");
    if (Request.HttpMethod == "POST")
    {
        if (Request.Form.Count > 0)
        {
            Response.Write(Request.Form[0] +"<br/>");
            Response.Write(Request.Form[1] + "<br/>");
            Response.Write(Request.Form[2] + "<br/>");
        }
    }
}

Here is a screenshot from fiddler :

在此处输入图片说明

Looks like you need to debug your app to see where the failure might be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM