Consulting

Results 1 to 2 of 2

Thread: XMLHTTP "POST" to an ASP page

  1. #1

    XMLHTTP "POST" to an ASP page

    I've been attempting to "Post" form data to ASP Login web pages using VBA.

    So far I've been unsuccessful on every site I attempt to do this with.

    I've successfully employed the use of Fiddler to accurately determine the format of the Request Header Data, and through trial and error the Request haders that are required for the Post method to accurately return, yet I still can't get an MSXMLHTTPRequest to be successful. I consistently get a -2147024809 (80070057) error. "The Parameter is Incorrect" at my .send line. In the example below, I've determeined with Fiddler that only three of theRequest headers are needed for the page to return correctly.

    Here is an example of the code I use to attempt a login at one site that requrires a __VIEWSTATE variable to be sent.

    [VBA]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 = "http://xyzabc.com/LoginPage.aspx"

    strViewState = getInitialViewState(strURL)
    strViewState = Replace(strViewState, "/", "%2F") ' Mimics the urlencode method

    strUName = "xxx"
    strPassword = "yyy"

    'Set up the Data String to Send

    strData = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" & strViewState & _
    "&ctl00%24ContentPlaceHolder1%24Login1%24Login1%24UserName=" & strUName & _
    "&ctl00%24ContentPlaceHolder1%24Login1%24Login1%24Password=" & strPassword & _
    "&ctl00%24ContentPlaceHolder1%24Login1%24Login1%24LoginImageButton.x=0" & _
    "&ctl00%24ContentPlaceHolder1%24Login1%24Login1%24LoginImageButton.y=0"

    'Post the Data
    Set httpreq = CreateObject("Microsoft.XMLHTTP")

    Call DeleteUrlCacheEntry(strURL)

    With httpreq
    .Open "POST", strURL, False
    .setRequestHeader "Host", "www.xyzabc.com"
    .setRequestHeader "Content-Length", Len(strData)
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

    .send strData 'ERROR HERE...

    Debug.Print .responseText
    End With

    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
    [/VBA]

    With the site in this example, I can get past the Login page by using an instance of Internet Explorer, setting the Uname and Password fields, and then doing a .click on the Button element. That works, but on most ASP Login pages, I'm not able to "click" the submit button via a brower object, thus my desire to perfect a Post Method.

    Anyone have any ideas on how to get the POST method to be successful?

    Any help much appreciated.

    Thanks...

  2. #2
    Looks LIke I figured it out. Sometimes it just takes posting it I guess.

    Solution:

    .send (strdata)

    I didn't have parenthesis around the strdata variable before. After that it worked like a charm. I hope someone else can use it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •