tags:

views:

4109

answers:

4

Using C# and ASP.NET I want to programmatically fill in some values (4 text boxes) on a web page (form) and then 'POST' those values. How do I do this?

Edit: Clarification: There is a service (www.stopforumspam.com) where you can submit ip, username and email address on their 'add' page. I want to be able to create a link/button on my site's page that will fill in those values and submit the info without having to copy/paste them across and click the submit button.

Further clarification: How do automated spam bots fill out forms and click the submit button if they were written in C#?

+1  A: 

Take a look at Watin

SQLMenace
+10  A: 

You can see a sample of that here: http://en.csharp-online.net/HTTP_Post

Basically, the code will look something like this:

WebRequest req = WebRequest.Create("http://mysite/myform.aspx");
string postData = "item1=11111&item2=22222&Item3=33333";

byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;

Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
Ryan Farley
Wouldn't the postData need to be urlencoded?
Bruno Lopes
Could you please explain how to collect the returned data from the request ?
Barbaros Alp
In the sample above, the returnvalue contains the data returned from the request.
Ryan Farley
+1  A: 

View the source of the page and use the WebRequest class to do the posting. No need to drive IE. Just figure out what IE is sending to the server and replicate that. Using a tool like Fiddler will make it even easier.

Ryan Rinaldi
A: 

Sorry but I'm not sure what the point here is. I understand that it should post data/submit a form but it doesn't work. What it goes is only returning page contents of the login page.

I'm very excited to find this page but it just doesn't cover its title. Can you please show us how to by pass the login page or ... maybe I'm not following your point here.

Any comments?

Regards.