PDA

View Full Version : excel VBA and post for ASP.NET



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!

tepkool01
02-09-2011, 12:53 PM
Anyone have any thoughts on this or guesses? I'm kinda out of guesses

tepkool01
02-09-2011, 12:54 PM
If you need any more information to help you figure this out...I'll be more than happy to give you what you need.

tepkool01
02-09-2011, 02:08 PM
Update:

I commented out: .setRequestHeader "Host", "URLWEBSITEBECAUSEFORUMWONTLETMEPOSTIT"
.setRequestHeader "Content-Length", Len(strData)
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

and i stopped receiving an error, however now it won't go to the 2nd page-- I am stuck on the first page. So the postback didn't work and I wasn't able to navigate to the new site.

tepkool01
02-09-2011, 02:30 PM
I've also put in variables into the actual queries such as:

strData = "__EVENTTARGET=ctl00%24ContentPlaceHolder1%24grdvwSummary&__EVENTARGUMENT=P age%242&__VIEWSTATE=" & strViewState ' & _
"&ctl00%24ContentPlaceHolder1%24grdvwSummary=Page%243"

to no avail. It keeps leading back to the home page.

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}

is in the HTML code so that's where I got my previous idea of the query actually changing...However I don't know what to do with LASTFOCUS nor do I know what it does...

Aside from all of this at least I'm getting a page and not an error. I just need to know how to get Page2.....