Posting using httpwebrequest

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Should the form submit button click event handler fire when posting to a
aspx page using httpwebrequest.

In my tests the load event fires when POSTing back but not the button click
event handler.
I first issue a GET, extract __VIEWSTATE and then POST back __VIEWSTATE and
the form values.

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
 
Are you posting any value for the button when posting the form values? My
guess is you would have to post something like "Button1=Button". Otherwise
the asp.net framework has no way of telling that the button has been
"clicked".

HTH, Jakob.
 
Yep, I'm posting Button1=Clicked, see below ...

viewState = SrcViewState 'a readonly property that gets __VIEWSTATE
postData.Append("__VIEWSTATE=")
postData.Append(viewState)
postData.Append("&Button1=Clicked")
Dim encodedPostData As String
encodedPostData = HttpUtility.UrlEncode(postData.ToString)
hrqURL = HttpWebRequest.Create(m_strURL)
hrqURL.ContentType = "application/x-www-form-urlencoded"
hrqURL.ContentLength = encodedPostData.Length
hrqURL.Method = "POST"
Dim cookies As New CookieContainer
hrqURL.CookieContainer = cookies
Dim requestWriter As New StreamWriter(hrqURL.GetRequestStream())
requestWriter.Write(EncodedPostData)
requestWriter.Close()
hrspURL = hrqURL.GetResponse()
 
I can not get it working when posting __VIEWSTATE (It results in an internal
server error). My suggestion is that you don't try to fire the Click event
of the button. Instead change your code to something like (assuming you
control the server code):

if (Request["Button1"] != null)
{
// do some work
}

Or perhaps a web service is the way to go?

Regards, Jakob.
 
Thanks again Jakob,

Scraping is my only option as I have no control over the source application
and it doesn't provide any webservice(s).

I would still like to known if the buttons click event should fire, is there
anyone out there you can help please ?


Jakob Christensen said:
I can not get it working when posting __VIEWSTATE (It results in an internal
server error). My suggestion is that you don't try to fire the Click event
of the button. Instead change your code to something like (assuming you
control the server code):

if (Request["Button1"] != null)
{
// do some work
}

Or perhaps a web service is the way to go?

Regards, Jakob.


Pazza said:
Yep, I'm posting Button1=Clicked, see below ...

viewState = SrcViewState 'a readonly property that gets __VIEWSTATE
postData.Append("__VIEWSTATE=")
postData.Append(viewState)
postData.Append("&Button1=Clicked")
Dim encodedPostData As String
encodedPostData = HttpUtility.UrlEncode(postData.ToString)
hrqURL = HttpWebRequest.Create(m_strURL)
hrqURL.ContentType = "application/x-www-form-urlencoded"
hrqURL.ContentLength = encodedPostData.Length
hrqURL.Method = "POST"
Dim cookies As New CookieContainer
hrqURL.CookieContainer = cookies
Dim requestWriter As New StreamWriter(hrqURL.GetRequestStream())
requestWriter.Write(EncodedPostData)
requestWriter.Close()
hrspURL = hrqURL.GetResponse()
 
Back
Top