• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Arranging formdata meant to be passed within post request in vba

shahin

Active Member
While dealing with ".aspx" or ".php" or sites with extension like that it's very painful to pass "payload" parameter within "post" request in order to get a valid response. Today when I tried to buid a scraper using python for a ".aspx" site, I could find out that the logic I applied in python can be applied in vba as well to save the energy from writing "__viewstate", "__eventtarget", "__eventargument" etc. manually in the formdata. My below script can populate them dynamically. However, the only thing I need to fix is place them in a single line concatinating "=" and "&" to be able to generate a valid response from that target site.

This is my script so far:
Code:
Sub GetInput()
    Dim HTML As New HTMLDocument
    Dim I&

    With New XMLHTTP60
        .Open "GET", "https://bccorreio.bcb.gov.br/bccorreio/Autenticacao/Logon.aspx?ReturnUrl=%2fbccorreio%2f", False
        .send
        HTML.body.innerHTML = .responseText
    End With

    With HTML.querySelectorAll("#form1 input")
        For I = 0 To .Length - 1
            R = R + 1: Cells(R, 1) = .item(I).getAttribute("name")
            Cells(R, 2) = .item(I).getAttribute("value")
        Next I
    End With
End Sub

The result it produces can be seen in the first text file attached below.

What I wish to do now is like this (few have values and few don't have):
Code:
__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=

How can i achive that? Thanks in advance.

FYI, the script is able to populate formdata (names and its corresponding values) from any site (the ones require post request). All it is needed to fix the "selector" to get them.
 

Attachments

  • payload output.txt
    608 bytes · Views: 3
Last edited:
I'm not sure what end result you are looking for... but it's simple string concatenation with Iif() for beginning of string.

Code:
    With HTML.querySelectorAll("#form1 input")
        For I = 0 To .Length - 1
            myString = IIf(Len(myString) = 0, .Item(I).getAttribute("name") & "=" & .Item(I).getAttribute("value"), _
                        myString & "&" & .Item(I).getAttribute("name") & "=" & .Item(I).getAttribute("value"))
        Next I
    End With
 
It's so perfect!!! This is it, sir. It would be very helpful for me if you could provide me with a oneliner explanation how it worked as I'm little behind when It comes to work using conditional statement. Thanks again, sir.
 
There is something else to modify in the below script as well which are the "username" and "password" portion. I can't find any idea how can the below script switch between the two variables "uname" and "pword" to fill in the "else block" automatically. Currently it is getting filled in with the "uname" twice as I can't manipulate it:
Code:
Sub GetInput()
    Dim HTML As New HTMLDocument
    Dim uname$, pword$, I&

    With New XMLHTTP60
        .Open "GET", "https://bccorreio.bcb.gov.br/bccorreio/Autenticacao/Logon.aspx?ReturnUrl=%2fbccorreio%2f", False
        .send
        HTML.body.innerHTML = .responseText
    End With
 
    uname = "shahin"
    pword = "12345"
 
    With HTML.querySelectorAll("#form1 input")
        For I = 0 To .Length - 1
            If Not (.Item(I).getAttribute("name") = "lgLogin$UserName" Or .Item(I).getAttribute("name") = "lgLogin$Password") Then
                R = R + 1: Cells(R, 1) = .Item(I).getAttribute("name")
                Cells(R, 2) = .Item(I).getAttribute("value")
         
            Else:
                R = R + 1: Cells(R, 1) = .Item(I).getAttribute("name")
                Cells(R, 2) = uname  'I wish the script will fill in this line with "pword" in it's second iteration upon meeting the else criterion.
            End If
        Next I
    End With
End Sub
 
oneliner explanation how it worked
If char length of string myString = 0, then concatenate name attribute, "=" and value attribute. Else, concatenate myString followed by, "&", name attribute, "=" and value attribute.
 
It seems I can get it done by adding "elseif block" but I expected to do the same limiting the condition within "if" and "else":
Code:
Sub GetInput()
    Dim HTML As New HTMLDocument, I&

    With New XMLHTTP60
        .Open "GET", "https://bccorreio.bcb.gov.br/bccorreio/Autenticacao/Logon.aspx?ReturnUrl=%2fbccorreio%2f", False
        .send
        HTML.body.innerHTML = .responseText
    End With

    With HTML.querySelectorAll("#form1 input")
        For I = 0 To .Length - 1
            If .Item(I).getAttribute("name") = "lgLogin$UserName" Then
                R = R + 1: Cells(R, 1) = .Item(I).getAttribute("name")
                Cells(R, 2) = "shahin"
               
            ElseIf .Item(I).getAttribute("name") = "lgLogin$Password" Then
                R = R + 1: Cells(R, 1) = .Item(I).getAttribute("name")
                Cells(R, 2) = "12345"
         
            Else:
                R = R + 1: Cells(R, 1) = .Item(I).getAttribute("name")
                Cells(R, 2) = .Item(I).getAttribute("value")
           End If
        Next I
    End With
End Sub
 
Do you mean something like that
Code:
Sub GetInput()
    Dim HTML As New HTMLDocument, i&, r&

    With New XMLHTTP60
        .Open "GET", "https://bccorreio.bcb.gov.br/bccorreio/Autenticacao/Logon.aspx?ReturnUrl=%2fbccorreio%2f", False
        .send
        HTML.body.innerHTML = .responseText
    End With

    With HTML.querySelectorAll("#form1 input")
        For i = 0 To .Length - 1
            r = r + 1: Cells(r, 1) = .Item(i).getAttribute("name")
            Cells(r, 2) = .Item(i).getAttribute("value")

            If .Item(i).getAttribute("name") = "lgLogin$UserName" Then
                Cells(r, 2) = "shahin"
            ElseIf .Item(i).getAttribute("name") = "lgLogin$Password" Then
                Cells(r, 2) = "12345"
            End If
        Next i
    End With
End Sub
 
Back
Top