How to simulate a form post?

A

Al Cadalzo

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
 
G

George Ter-Saakov

"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 said:
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
 
D

Dino Chiesa [Microsoft]

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 said:
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
 
A

Al Cadalzo

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 said:
"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 said:
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
 
A

Al Cadalzo

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 said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top