tepkool01
02-08-2011, 08:47 PM
Hey I was reading Shred Dude's code about the ASP.NET posting in VBA (/forum/showthread.php?t=28756). I am trying to do the same thing right now and tried to attach that code to my application. I tried some guesses but keep getting an error. I was wondering if anyone had an idea of what I could do to fix it...
Unfortunately you can't view the website because it is on my company's server, so I will post the source code. What I have is a crawler application in VBA that searches via regex for links on a page, opens the page, and then grabs data from that page to put into excel (it does this for all the links on the page within a certain table). Now of course this is a lot of data and there are several pages of it. When you click on page 2 to see the next table of links where I can extract data from, the link is actually a post method in ASP.NET which makes my life infinitely more difficult.
From Shred Dude's code, I added in __LASTFOCUS and instead of usernames I just put in the thing for the page2 which translates into Page%242....Anyways I keep getting that glorious error, and I've checked the viewstate's and they seem to match up with what the website is getting. I am trying to get the content of page2, page 3, etc so I can continue to extract ALL the data in all the pages. In order to do that I need the contents of page 2, page 3, etc...My other module will do the rest.
Here is the code that I extracted from Shred Dude's and tried to make modifications to (THIS DOES NOT WORK):
Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Public Sub PostToASP()
Dim strURL As String
Dim strViewState As String, strEventValidation As String
Dim strProxy As String
Dim strData As String
Dim byteData As Byte
Dim httpreq As Object
Dim strUName As String
Dim strPassword As String
''''Find inititial ViewState Property
strURL = "URLWEBSITEBECAUSEFORUMWONTLETMEPOSTIT"
strViewState = getInitialViewState(strURL)
strViewState = Replace(strViewState, "/", "%2F") ' Mimics the urlencode method
Cells(1, 1).value = strViewState
strUName = "xxx"
strPassword = "yyy"
'Set up the Data String to Send
strData = "__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=" & strViewState & _
"&ctl00%24ContentPlaceHolder1%24grdvwSummary=Page%242"
Cells(3, 1).value = strData
'Post the Data
Set httpreq = CreateObject("Microsoft.XMLHTTP")
Call DeleteUrlCacheEntry(strURL)
With httpreq
.Open "POST", strURL, False
.setRequestHeader "Host", "URLWEBSITEBECAUSEFORUMWONTLETMEPOSTIT"
.setRequestHeader "Content-Length", Len(strData)
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send (strData) 'ERROR HERE...
'Debug.Print .responseText
Cells(5, 1).value = .responseText
End With
'Cells(5, 1).value = .responseText
Set httpreq = Nothing
End Sub
Public Function getInitialViewState(strURL As String) As String
Dim objMSHTML As New MSHTML.HTMLDocument
Dim MyDoc As MSHTML.HTMLDocument
Call DeleteUrlCacheEntry(strURL)
Set MyDoc = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
While MyDoc.ReadyState <> "complete"
DoEvents
Wend
getInitialViewState = MyDoc.getElementById("__VIEWSTATE").value
Set objMSHTML = Nothing
Set MyDoc = Nothing
End Function
I left in a bunch of Shred Dude's old code so some of the variables and stuff doesn't make sense, however I tried to make it so it SHOULD still work. I don't really understand too much of ASP.NET so I was taking guesses with changing things. If you want to see Shred Dude's original code, check out the link in my first paragraph.
The source code of the webpage I'm trying to open the 2nd, 3rd, 4th pages from are as follows:
(saved as chd.docx)
The content with all the 'links' is as follows:
<td><span>1</span></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$2')">2</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$3')">3</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$4')">4</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$5')">5</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$6')">6</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$7')">7</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$8')">8</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$9')">9</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder
so I hope that saves a little time, however I had to dig through to find the part about __LASTFOCUS
Let me know what you think!
Unfortunately you can't view the website because it is on my company's server, so I will post the source code. What I have is a crawler application in VBA that searches via regex for links on a page, opens the page, and then grabs data from that page to put into excel (it does this for all the links on the page within a certain table). Now of course this is a lot of data and there are several pages of it. When you click on page 2 to see the next table of links where I can extract data from, the link is actually a post method in ASP.NET which makes my life infinitely more difficult.
From Shred Dude's code, I added in __LASTFOCUS and instead of usernames I just put in the thing for the page2 which translates into Page%242....Anyways I keep getting that glorious error, and I've checked the viewstate's and they seem to match up with what the website is getting. I am trying to get the content of page2, page 3, etc so I can continue to extract ALL the data in all the pages. In order to do that I need the contents of page 2, page 3, etc...My other module will do the rest.
Here is the code that I extracted from Shred Dude's and tried to make modifications to (THIS DOES NOT WORK):
Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Public Sub PostToASP()
Dim strURL As String
Dim strViewState As String, strEventValidation As String
Dim strProxy As String
Dim strData As String
Dim byteData As Byte
Dim httpreq As Object
Dim strUName As String
Dim strPassword As String
''''Find inititial ViewState Property
strURL = "URLWEBSITEBECAUSEFORUMWONTLETMEPOSTIT"
strViewState = getInitialViewState(strURL)
strViewState = Replace(strViewState, "/", "%2F") ' Mimics the urlencode method
Cells(1, 1).value = strViewState
strUName = "xxx"
strPassword = "yyy"
'Set up the Data String to Send
strData = "__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=" & strViewState & _
"&ctl00%24ContentPlaceHolder1%24grdvwSummary=Page%242"
Cells(3, 1).value = strData
'Post the Data
Set httpreq = CreateObject("Microsoft.XMLHTTP")
Call DeleteUrlCacheEntry(strURL)
With httpreq
.Open "POST", strURL, False
.setRequestHeader "Host", "URLWEBSITEBECAUSEFORUMWONTLETMEPOSTIT"
.setRequestHeader "Content-Length", Len(strData)
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send (strData) 'ERROR HERE...
'Debug.Print .responseText
Cells(5, 1).value = .responseText
End With
'Cells(5, 1).value = .responseText
Set httpreq = Nothing
End Sub
Public Function getInitialViewState(strURL As String) As String
Dim objMSHTML As New MSHTML.HTMLDocument
Dim MyDoc As MSHTML.HTMLDocument
Call DeleteUrlCacheEntry(strURL)
Set MyDoc = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
While MyDoc.ReadyState <> "complete"
DoEvents
Wend
getInitialViewState = MyDoc.getElementById("__VIEWSTATE").value
Set objMSHTML = Nothing
Set MyDoc = Nothing
End Function
I left in a bunch of Shred Dude's old code so some of the variables and stuff doesn't make sense, however I tried to make it so it SHOULD still work. I don't really understand too much of ASP.NET so I was taking guesses with changing things. If you want to see Shred Dude's original code, check out the link in my first paragraph.
The source code of the webpage I'm trying to open the 2nd, 3rd, 4th pages from are as follows:
(saved as chd.docx)
The content with all the 'links' is as follows:
<td><span>1</span></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$2')">2</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$3')">3</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$4')">4</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$5')">5</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$6')">6</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$7')">7</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$8')">8</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdvwSummary','Page$9')">9</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder
so I hope that saves a little time, however I had to dig through to find the part about __LASTFOCUS
Let me know what you think!