364,033 Members | 4774 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Submit a HTML Form programmatically

Darran
P: n/a
Darran
Hi

I want to be able to retrieve the data from a given URL, fill in the
necessary form fields and submit the form back to the server, all from
within a Windows Application. I can retrieve the HTML from the page,
using the HttpWebRequest class but that is where I have become stuck.
These are the remaining items I would like to be able to do

1 Fill in the necessary fields on the form.
2 Submit the data back to the server
3 Retrieve the response from the server and start the loop again.

Any ideas?


Thanks

Darran
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


Nicholas Paldino [.NET/C# MVP]
P: n/a
Nicholas Paldino [.NET/C# MVP]
Darran,

You will use the HttpWebRequest class just as you did before. However,
this time, you need to set the Method property to "POST", which will
indicate that you are posting data. Also, you will have to call
GetRequestStream, and write to the stream the content of the forms data.

First, you need to set the ContentType property to
"application/x-www-form-urlencoded".

Then, you have to find the ids of the controls that are going to send
information. You will have to then write to the Stream returned by
GetRequestStream the values in the following manner:

name1=value1&name2=value2

You have to make sure that the values are encoded correctly, as certain
characters are not allowed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com

"Darran" <DarranShelton@hotmail.com> wrote in message
news:4ff3a84.0307071216.6c13332c@posting.google.co m...[color=blue]
> Hi
>
> I want to be able to retrieve the data from a given URL, fill in the
> necessary form fields and submit the form back to the server, all from
> within a Windows Application. I can retrieve the HTML from the page,
> using the HttpWebRequest class but that is where I have become stuck.
> These are the remaining items I would like to be able to do
>
> 1 Fill in the necessary fields on the form.
> 2 Submit the data back to the server
> 3 Retrieve the response from the server and start the loop again.
>
> Any ideas?
>
>
> Thanks
>
> Darran[/color]


Nov 13 '05 #2

Nicholas Paldino [.NET/C# MVP]
P: n/a
Nicholas Paldino [.NET/C# MVP]
Darran,

Or, you can just use the UploadValues method on the WebClient class.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com

"Darran" <DarranShelton@hotmail.com> wrote in message
news:4ff3a84.0307071216.6c13332c@posting.google.co m...[color=blue]
> Hi
>
> I want to be able to retrieve the data from a given URL, fill in the
> necessary form fields and submit the form back to the server, all from
> within a Windows Application. I can retrieve the HTML from the page,
> using the HttpWebRequest class but that is where I have become stuck.
> These are the remaining items I would like to be able to do
>
> 1 Fill in the necessary fields on the form.
> 2 Submit the data back to the server
> 3 Retrieve the response from the server and start the loop again.
>
> Any ideas?
>
>
> Thanks
>
> Darran[/color]


Nov 13 '05 #3

MyWay
P: 3
I have to do a similar thing: fill 2 textbox with user and password, can you explain how to do it?
Jun 12 '06 #4

MyWay
P: 3
Uhm.. Nobody knows? :(
Jun 12 '06 #5

MyWay
P: 3
Expand|Select|Wrap|Line Numbers
  1.                         ASCIIEncoding encoding = new ASCIIEncoding();
  2.                         string postData = "username=" + strUser + "&password=" + strPass + "&login_button.x=0&login_button.y=0";
  3.                         byte[] data = encoding.GetBytes(postData);
  4.                         // Prepare web request...
  5.                         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("urlofthesite");
  6.                         myRequest.Method = "POST";
  7.                         myRequest.ContentType = "text/html";
  8.                         myRequest.ContentLength = data.Length;
  9.                         Stream newStream = myRequest.GetRequestStream();
  10.                         // Send the data.
  11.                         newStream.Write(data, 0, data.Length);
  12.                         newStream.Close();
  13.  
I find out this code, but how can i check if i'm logged in succesfully or not?
Jun 13 '06 #6

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp