How to simulate a form post?

Discussion in 'ASP .Net' started by Al Cadalzo, Feb 17, 2004.

  1. Al Cadalzo

    Al Cadalzo Guest

    I'm trying to simulate a form post (i.e. Method="POST").

    The FORM POST I'm trying to simulate is similar to this:

    <FORM NAME=SearchForm METHOD=POST ACTION=Search>
    <SELECT name="criteriaA" >
    <OPTION VALUE=1>AAAAAAAAAAA</OPTION>
    <OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
    </SELECT>
    ....
    </FORM>

    Here is my code:
    >>

    Uri httpSite = new Uri(url);

    WebRequest wreq = WebRequest.Create(httpSite);

    wreq.ContentType = "application/x-www-form-urlencoded";
    wreq.Method = "POST";
    string formvars = string.Empty;
    formvars = "criteriaA=1&criteriaB=2";

    Byte[] byteArray = Encoding.UTF8.GetBytes(formvars);

    wreq.ContentLength = byteArray.Length;
    Stream reqStream = wreq.GetRequestStream();
    reqStream.Write(byteArray, 0, byteArray.Length);

    reqStream.Close();
    <<

    Am I specifying the form variables correctly?

    The FORM also has an ACTION=Search
    How do I pass the ACTION=Search in my WebRequest?

    I'm getting a response back with html of the page I'm requesting but it does
    not have any search results.
    The area of the page where the search results would appear is blank.

    Any help or hints anyone can provide are appreciated.

    Thanks,
    Al
    Al Cadalzo, Feb 17, 2004
    #1
    1. Advertising

  2. "Action" is actually the page where you submit the form.
    So when you clicking submit button browser sents the POST request to the
    page specified in "Action"

    So in your case you must use
    WebRequest wreq = WebRequest.Create(http://www.mysite.com/Search);

    George.

    "Al Cadalzo" <> wrote in message
    news:...
    > I'm trying to simulate a form post (i.e. Method="POST").
    >
    > The FORM POST I'm trying to simulate is similar to this:
    >
    > <FORM NAME=SearchForm METHOD=POST ACTION=Search>
    > <SELECT name="criteriaA" >
    > <OPTION VALUE=1>AAAAAAAAAAA</OPTION>
    > <OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
    > </SELECT>
    > ...
    > </FORM>
    >
    > Here is my code:
    > >>

    > Uri httpSite = new Uri(url);
    >
    > WebRequest wreq = WebRequest.Create(httpSite);
    >
    > wreq.ContentType = "application/x-www-form-urlencoded";
    > wreq.Method = "POST";
    > string formvars = string.Empty;
    > formvars = "criteriaA=1&criteriaB=2";
    >
    > Byte[] byteArray = Encoding.UTF8.GetBytes(formvars);
    >
    > wreq.ContentLength = byteArray.Length;
    > Stream reqStream = wreq.GetRequestStream();
    > reqStream.Write(byteArray, 0, byteArray.Length);
    >
    > reqStream.Close();
    > <<
    >
    > Am I specifying the form variables correctly?
    >
    > The FORM also has an ACTION=Search
    > How do I pass the ACTION=Search in my WebRequest?
    >
    > I'm getting a response back with html of the page I'm requesting but it

    does
    > not have any search results.
    > The area of the page where the search results would appear is blank.
    >
    > Any help or hints anyone can provide are appreciated.
    >
    > Thanks,
    > Al
    >
    >
    George Ter-Saakov, Feb 17, 2004
    #2
    1. Advertising

  3. The form action is the URL to which the form data should be submitted.

    So your url should be "Search", eg

    "http://something.org/Search"

    (whatever the releative URL "Search" resolves to.)


    "Al Cadalzo" <> wrote in message
    news:...
    > I'm trying to simulate a form post (i.e. Method="POST").
    >
    > The FORM POST I'm trying to simulate is similar to this:
    >
    > <FORM NAME=SearchForm METHOD=POST ACTION=Search>
    > <SELECT name="criteriaA" >
    > <OPTION VALUE=1>AAAAAAAAAAA</OPTION>
    > <OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
    > </SELECT>
    > ...
    > </FORM>
    >
    > Here is my code:
    > >>

    > Uri httpSite = new Uri(url);
    >
    > WebRequest wreq = WebRequest.Create(httpSite);
    >
    > wreq.ContentType = "application/x-www-form-urlencoded";
    > wreq.Method = "POST";
    > string formvars = string.Empty;
    > formvars = "criteriaA=1&criteriaB=2";
    >
    > Byte[] byteArray = Encoding.UTF8.GetBytes(formvars);
    >
    > wreq.ContentLength = byteArray.Length;
    > Stream reqStream = wreq.GetRequestStream();
    > reqStream.Write(byteArray, 0, byteArray.Length);
    >
    > reqStream.Close();
    > <<
    >
    > Am I specifying the form variables correctly?
    >
    > The FORM also has an ACTION=Search
    > How do I pass the ACTION=Search in my WebRequest?
    >
    > I'm getting a response back with html of the page I'm requesting but it

    does
    > not have any search results.
    > The area of the page where the search results would appear is blank.
    >
    > Any help or hints anyone can provide are appreciated.
    >
    > Thanks,
    > Al
    >
    >
    Dino Chiesa [Microsoft], Feb 17, 2004
    #3
  4. Al Cadalzo

    Al Cadalzo Guest

    George,
    Thanks. I added that to the URL, but I'm still getting a blank section of
    the response where the results would appear.
    I can see most of the page except where the results would appear. I must be
    missing one or more of the required form parameters.

    Thanks,
    Al

    "George Ter-Saakov" <> wrote in message
    news:...
    > "Action" is actually the page where you submit the form.
    > So when you clicking submit button browser sents the POST request to the
    > page specified in "Action"
    >
    > So in your case you must use
    > WebRequest wreq = WebRequest.Create(http://www.mysite.com/Search);
    >
    > George.
    >
    > "Al Cadalzo" <> wrote in message
    > news:...
    > > I'm trying to simulate a form post (i.e. Method="POST").
    > >
    > > The FORM POST I'm trying to simulate is similar to this:
    > >
    > > <FORM NAME=SearchForm METHOD=POST ACTION=Search>
    > > <SELECT name="criteriaA" >
    > > <OPTION VALUE=1>AAAAAAAAAAA</OPTION>
    > > <OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
    > > </SELECT>
    > > ...
    > > </FORM>
    > >
    > > Here is my code:
    > > >>

    > > Uri httpSite = new Uri(url);
    > >
    > > WebRequest wreq = WebRequest.Create(httpSite);
    > >
    > > wreq.ContentType = "application/x-www-form-urlencoded";
    > > wreq.Method = "POST";
    > > string formvars = string.Empty;
    > > formvars = "criteriaA=1&criteriaB=2";
    > >
    > > Byte[] byteArray = Encoding.UTF8.GetBytes(formvars);
    > >
    > > wreq.ContentLength = byteArray.Length;
    > > Stream reqStream = wreq.GetRequestStream();
    > > reqStream.Write(byteArray, 0, byteArray.Length);
    > >
    > > reqStream.Close();
    > > <<
    > >
    > > Am I specifying the form variables correctly?
    > >
    > > The FORM also has an ACTION=Search
    > > How do I pass the ACTION=Search in my WebRequest?
    > >
    > > I'm getting a response back with html of the page I'm requesting but it

    > does
    > > not have any search results.
    > > The area of the page where the search results would appear is blank.
    > >
    > > Any help or hints anyone can provide are appreciated.
    > >
    > > Thanks,
    > > Al
    > >
    > >

    >
    >
    Al Cadalzo, Feb 17, 2004
    #4
  5. Al Cadalzo

    Al Cadalzo Guest

    Dino,

    Thanks for your help. Still not getting the expected page results. Maybe
    I'm missing a form variable. I'm going to try using WebClient.UploadValues.

    Thanks,
    Al


    "Dino Chiesa [Microsoft]" <> wrote in message
    news:...
    > The form action is the URL to which the form data should be submitted.
    >
    > So your url should be "Search", eg
    >
    > "http://something.org/Search"
    >
    > (whatever the releative URL "Search" resolves to.)
    >
    >
    > "Al Cadalzo" <> wrote in message
    > news:...
    > > I'm trying to simulate a form post (i.e. Method="POST").
    > >
    > > The FORM POST I'm trying to simulate is similar to this:
    > >
    > > <FORM NAME=SearchForm METHOD=POST ACTION=Search>
    > > <SELECT name="criteriaA" >
    > > <OPTION VALUE=1>AAAAAAAAAAA</OPTION>
    > > <OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
    > > </SELECT>
    > > ...
    > > </FORM>
    > >
    > > Here is my code:
    > > >>

    > > Uri httpSite = new Uri(url);
    > >
    > > WebRequest wreq = WebRequest.Create(httpSite);
    > >
    > > wreq.ContentType = "application/x-www-form-urlencoded";
    > > wreq.Method = "POST";
    > > string formvars = string.Empty;
    > > formvars = "criteriaA=1&criteriaB=2";
    > >
    > > Byte[] byteArray = Encoding.UTF8.GetBytes(formvars);
    > >
    > > wreq.ContentLength = byteArray.Length;
    > > Stream reqStream = wreq.GetRequestStream();
    > > reqStream.Write(byteArray, 0, byteArray.Length);
    > >
    > > reqStream.Close();
    > > <<
    > >
    > > Am I specifying the form variables correctly?
    > >
    > > The FORM also has an ACTION=Search
    > > How do I pass the ACTION=Search in my WebRequest?
    > >
    > > I'm getting a response back with html of the page I'm requesting but it

    > does
    > > not have any search results.
    > > The area of the page where the search results would appear is blank.
    > >
    > > Any help or hints anyone can provide are appreciated.
    > >
    > > Thanks,
    > > Al
    > >
    > >

    >
    >
    Al Cadalzo, Feb 17, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. delph
    Replies:
    3
    Views:
    1,306
    Andrea Desole
    May 13, 2005
  2. dast

    Simulate post at webpage

    dast, Sep 15, 2005, in forum: C++
    Replies:
    2
    Views:
    306
    =?ISO-8859-1?Q?Stefan_N=E4we?=
    Sep 15, 2005
  3. BarryX

    Simulate Form Post on Server Side

    BarryX, Sep 26, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    505
    Patrice
    Sep 26, 2007
  4. nospam
    Replies:
    1
    Views:
    426
    sloan
    Oct 13, 2007
  5. BarryX

    Simulate form post from Server Side

    BarryX, Sep 26, 2007, in forum: ASP General
    Replies:
    9
    Views:
    379
    Anthony Jones
    Oct 2, 2007
Loading...

Share This Page