POST to another page simulating click

  • Thread starter Alhambra Eidos Kiquenet
  • Start date
A

Alhambra Eidos Kiquenet

Hi misters,

I want to submit form in a console application to simulate user click
button to submit in browser
There are controls in page. Four TextBoxs and a Button (type submit).

And i have registered an event for button to handle the request:

void button_Click(object sender, EventArgs e)
{
Response.Write(userName.Text); //or do something
}

We can input data in TextBoxs, then click the button to submit it. The web
application will process the request

What i want to do is do it in an application.(see code below)

static void Main(string[] args)
{

string url = "http://localhost/contact.aspx";
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

NameValueCollection values = new NameValueCollection();
values.Add("userName","xxx");
values.Add("email","xxx");
values.Add("company","xxx");
values.Add("comments","xxx");
values.Add("bSend","send");

byte[] r = client.UploadValues(url, "post", values);
}



But the application only fire the Page_Load event and doesn't raise the
Button.Click event.

any suggestions?
thanks in advance, any help will be very appreciated, regards.
 
B

bruce barker

to fire the button event you need to send two fields

1) the button name & value
2) "__VIEWSTATE" and a valid value

to get a valid viewstate to post, your console app needs to do a get of the
page and parse the html for the viewstate value. it can then use this value
for the next post.

note: you can not save the veiwstate for future use, you must ask for a
current value before the post.

-- bruce (sqlwork.com)
 
A

Alhambra Eidos Kiquenet

Thanks mister,
ok, I get the text of all page and parser it.

any HTML Parser about it (for get input VIEWSTATE, and another inputs...) ?

Regards.
 
C

Cowboy \(Gregory A. Beamer\)

I typed up a bit of an answer and planned on playing this one through and
then asked myself "what are you doing?" The reason is I thought through the
problem and could not see a logical reason why it HAD to be done this way.
The caps on HAD are very important. Let me illustrate.

First, let's look at classes. This could be a class library or a console
application; it really does not matter. But, here is a normal set of actions
on a class.

Class1 cls1 = new Class1();
cls1.InitialAction();
cls1.Save();

In windows forms or web forms, it is the same, only the input is coming from
user actions, so you have events that may hold the code for the actions (not
wise, as you should move business into its own classes, but it is doable in
..NET in both windows and web forms).

What you have asked is for it to work like this:

Class1 cls1 = new Class1();
Class2 cls2 = new Class2();

cls1.InitialAction();
cls2.SaveForClass1();

This is possible in a couple of situations

1. Class 1 Instantiates class 2
2. Class 2 is Instantiated with information from Class 1
3. Class 2 created class 1 (although this only works if
4. Class 2 is used as a link between data and business object (of which
class 1 is one)
5. There is a Class 3 that controls both Class 1 and Class 2

Many of the above are antipatterns. Although possible, they are not wise to
use. The web opens up additional possibilities, as it is stateless. In
theory, you can call any page (class) you desire with information from
another page (class). It is still an antipattern.

So, then I thought of reasons you might want to do this.

1. You have the same basic functionality in two pages if you do not.

Solution:
Move the save functionality into its own class and call from both location

2. You have the same bits on a form as well as same backend functionality

Solution:
Move both UI and code into a User Control. I would still considering moving
the actual business rules to a library anyway, but this is better than
posting to another page.

3. You want to show a confirmation

Solutions:

A) Forward after you save
B) Use Panel controls



Alhambra Eidos Kiquenet said:
Hi misters,

I want to submit form in a console application to simulate user click
button to submit in browser
There are controls in page. Four TextBoxs and a Button (type submit).

And i have registered an event for button to handle the request:

void button_Click(object sender, EventArgs e)
{
Response.Write(userName.Text); //or do something
}

We can input data in TextBoxs, then click the button to submit it. The web
application will process the request

What i want to do is do it in an application.(see code below)

static void Main(string[] args)
{

string url = "http://localhost/contact.aspx";
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

NameValueCollection values = new NameValueCollection();
values.Add("userName","xxx");
values.Add("email","xxx");
values.Add("company","xxx");
values.Add("comments","xxx");
values.Add("bSend","send");

byte[] r = client.UploadValues(url, "post", values);
}



But the application only fire the Page_Load event and doesn't raise the
Button.Click event.

any suggestions?
thanks in advance, any help will be very appreciated, regards.


--
http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.net
http://www.setbb.com/putainformatica/viewtopic.php?p=843
www.trabajobasura.com/solusoft
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top