Programmatic HTTP Post returns Empty Form fields on ASPX Server Side

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ERobishaw

    #1

    Programmatic HTTP Post returns Empty Form fields on ASPX Server Side

    Using Winform app writing a HTTP Post using the following... on the
    server side, I get no form fields. Request.Form.Al lKeys.Length = 0.

    If I put the field/value paris in the URL I can use
    Request["FieldName"] to retrieve the values,
    but POST doesn't work.

    There is NO Proxy... the only thing I can think is that ASP.NET is
    somehow filtering out the page because I'm obviously not posting the
    viewstate??? Seems like there was some setting for this, but I can't
    recall what it is.


    HttpWebRequest request = (HttpWebRequest )
    HttpWebRequest. Create("http://localhost/test.aspx");
    WebResponse response;

    string commentField = "ABCD=123";

    ASCIIEncoding encoding = new ASCIIEncoding() ;
    byte[] data = encoding.GetByt es(commentField );
    request.Method = "POST";
    request.Content Type = "applicatio n/x-www-form-urlencoded";
    request.Content Length = data.Length;
    Stream stream = request.GetRequ estStream();
    stream.Write(da ta, 0, data.Length);
    stream.Close();
    response = request.GetResp onse();


    //SERVER SIDE, test.aspx page
    Page_Load()
    {
    //BREAK POINT SET HERE>>>>
    //Request.Form.Al lKeys returns nothing (0 length)
    }
  • Anthony Jones

    #2
    Re: Programmatic HTTP Post returns Empty Form fields on ASPX Server Side

    "ERobishaw" <ERobishaw@gmai l.comwrote in message
    news:4c10225e-d8b6-48ee-8480-98e3350c15a3@e2 3g2000prf.googl egroups.com...
    Using Winform app writing a HTTP Post using the following... on the
    server side, I get no form fields. Request.Form.Al lKeys.Length = 0.
    >
    If I put the field/value paris in the URL I can use
    Request["FieldName"] to retrieve the values,
    but POST doesn't work.
    >
    There is NO Proxy... the only thing I can think is that ASP.NET is
    somehow filtering out the page because I'm obviously not posting the
    viewstate??? Seems like there was some setting for this, but I can't
    recall what it is.
    >
    >
    HttpWebRequest request = (HttpWebRequest )
    HttpWebRequest. Create("http://localhost/test.aspx");
    WebResponse response;
    >
    string commentField = "ABCD=123";
    >
    ASCIIEncoding encoding = new ASCIIEncoding() ;
    byte[] data = encoding.GetByt es(commentField );
    request.Method = "POST";
    request.Content Type = "applicatio n/x-www-form-urlencoded";
    request.Content Length = data.Length;
    Stream stream = request.GetRequ estStream();
    stream.Write(da ta, 0, data.Length);
    stream.Close();
    response = request.GetResp onse();
    >
    >
    //SERVER SIDE, test.aspx page
    Page_Load()
    {
    //BREAK POINT SET HERE>>>>
    //Request.Form.Al lKeys returns nothing (0 length)
    }
    Viewstate won't impact this code. The request object is a lower level
    object provided by the HttpContext that is present in any handler be that an
    ASPX or ASHX etc.

    I can't explain why AllKeys has a length of 0, your calling code looks good.
    Have you tried the System.Net.WebC lient object instead. It handles a lot of
    this stuff for you:-

    NameValueCollec tion fields = new NameValueCollec tion();
    WebClient client = new WebClient();
    fields.Add("ABC D", "123");
    String response = client.UploadVa lues("http://localhost/test.aspx", fields);



    --
    Anthony Jones - MVP ASP/ASP.NET


    Comment

    Working...