POSTing using httpwebrequest

G

Guest

Hi,

Is there a way to cause the form submit button click event handler to fire
when posting to a aspx page using httpwebrequest.

In my tests the load event fires but not by button click event. I am hoping
to use this technique for a screen scraper but if I can't get the submit
buttons click event handler to fire its no use.

Thanks,
Pazza
 
B

bruce barker

to post an aspx page you need to post a valid __viewstate from the render.
in this hidden field is the signal that the page is a postback. so you need
to do a GET, parse the viewstate value, and include it on the POST.

-- bruce (sqlwork.com)


| Hi,
|
| Is there a way to cause the form submit button click event handler to fire
| when posting to a aspx page using httpwebrequest.
|
| In my tests the load event fires but not by button click event. I am
hoping
| to use this technique for a screen scraper but if I can't get the submit
| buttons click event handler to fire its no use.
|
| Thanks,
| Pazza
|
 
G

Guest

Bruce,

Thank you for replying.
I'm doing what you suggest ie. a get, extracting __VIEWSTATE and then
posting it back. I'm a right in assuming that the submit buttons click event
handler should fire it this scenario ?

Pazza.
 
B

bruce barker

as long as you pass the submit button name and its value, like the browser
would on a button click. if the button is not a submit, then you need to
fill in the hidden field __target with the control name.

-- bruce (sqlwork.com)

| Bruce,
|
| Thank you for replying.
| I'm doing what you suggest ie. a get, extracting __VIEWSTATE and then
| posting it back. I'm a right in assuming that the submit buttons click
event
| handler should fire it this scenario ?
|
| Pazza.
|
| "bruce barker" wrote:
|
| > to post an aspx page you need to post a valid __viewstate from the
render.
| > in this hidden field is the signal that the page is a postback. so you
need
| > to do a GET, parse the viewstate value, and include it on the POST.
| >
| > -- bruce (sqlwork.com)
| >
| >
| > | > | Hi,
| > |
| > | Is there a way to cause the form submit button click event handler to
fire
| > | when posting to a aspx page using httpwebrequest.
| > |
| > | In my tests the load event fires but not by button click event. I am
| > hoping
| > | to use this technique for a screen scraper but if I can't get the
submit
| > | buttons click event handler to fire its no use.
| > |
| > | Thanks,
| > | Pazza
| > |
| >
| >
| >
 
G

Guest

Thanks for this Bruce,

Now that you have confirmed that the click event should fire I just need to
work out why it doesn't.
Here is a section of my code, can you see anything wrong with it ?

viewState = SrcViewState ' a readonly property that extracts __VIEWSTATE
postData.Append("__VIEWSTATE=")
postData.Append(viewState)
postData.Append("&Button1=Clicked")
Dim encodedPostData As String
encodedPostData = HttpUtility.UrlEncode(postData.ToString)

hrqURL = HttpWebRequest.Create("http://www.apage.aspx")
hrqURL.ContentType = "application/x-www-form-urlencoded"
hrqURL.ContentLength = EncodedPostData.Length
hrqURL.Method = "POST"
Dim requestWriter As New StreamWriter(hrqURL.GetRequestStream())
requestWriter.Write(EncodedPostData)
requestWriter.Close()
hrspURL = hrqURL.GetResponse()
 
G

Guest

Bruce,

I've noticed that the session id used for my GET (to extract __VIEWSTATE) is
different to that used for my POST - is this the problem ? and how do I solve
it ?
 
J

Joerg Jooss

Pazza said:
Bruce,

I've noticed that the session id used for my GET (to extract
__VIEWSTATE) is different to that used for my POST - is this the
problem ? and how do I solve it ?

Are you using a CookieContainer to capture and resend all cookies?

Cheers,
 
J

Joerg Jooss

Pazza said:
Hi,

No I am not, can you give me an example please.

Simply create a CookieContainer object and set HttpWebRequest's
CookieContainer property to this instance for each request in your HTTP
conversation. It will automatically track all cookies received in
HttpWebResponses.

Cheers,
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top