Excel VBA MSXML2.XMLHTTP unable to login to website with form (method=post)

拈花ヽ惹草 提交于 2020-08-10 19:37:40

问题


I have written a piece of code to scrape HTML of a website that requires login. Originally I used IE automation but it was painfully slow so I'm exploring other option including MSXML2.XMLHTTP.

I can login to website without any issues with IE but can't make it work with MSXML2.XMLHTTP.

This works ok:

   
With ieDoc.Forms(0)
       .textLogin.Value = GPROUsername
       .textPassword.Value = GPROPassword
       
       .submit
   End With

Unable to login with this one:

Sub TestHTTPLogin()
Dim XMLHttpRequest As Object

LoginForm.Show

Set XMLHttpRequest = CreateObject("MSXML2.XMLHTTP")
    With XMLHttpRequest
        .Open "POST", "https://gpro.net/gb/Login.asp?langCode=gb&Redirect=gpro.asp", False
        .setRequestHeader "Content-Type", "content=text/html; charset=UTF-8"
        .send "textLogin=" & GPROUsername & "&" _
          & "textPassword=" & GPROPassword
    
    Debug.Print .responseText
   
    End With

  Set XMLHttpRequest = Nothing

End Sub

This is the HTML of the target website

<form method="post" action="Login.asp?langCode=gb&Redirect=gpro.asp" style="margin:0px" ID="Form1">
        
        <tr>
            <td>Username or Email address:</td>
            <td class="leftalign"><input type="text" name="textLogin" autofocus value="" class="pad" style="font-size:16px;padding:4px !Important;border-radius:5px;border:1px solid #348bf8;margin-top:3px;width:180px"></td>
        </tr>
        <tr>
            <td align="right">Password:</td>
            <td><input type="password" name="textPassword" ID="Password1" class="pad" style="font-size:16px;padding:4px !Important;border-radius:5px;border:1px solid #348bf8;margin-top:3px;width:180px"> <a href="LostPassword.asp" class="password"> Lost password?</a></td>
        </tr>        
        <tr>
            <td></td>
            <td>
                <input type="hidden" name="token" value="" ID="token">
                <input type="hidden" name="Logon" value="Login">
                <input type="submit" name="LogonFake" value="Sign in" class="halo micro">
            </td>
        </tr>
        
        </table>
</form>

I decided to use WinHTTPRequest instead. Code below works ok.

LoginForm1 = "textLogin=" & GPROUsername & "&" & "textPassword=" & GPROPassword & "&" & "Logon=Login"
 
Set HttpRequest = CreateObject("WINHTTP.WinHTTPRequest.5.1")
Set Managerhtml = New HTMLDocument
With HttpRequest
        .Open "POST", "https://gpro.net/gb/Login.asp?langCode=gb&Redirect=gpro.asp", False
        .setRequestHeader "User-Agent", "GO"
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded", "charset=UTF-8"
        .setRequestHeader "Host", "gpro.net"
        .setRequestHeader "Content-Length", Len(LoginForm1)
        .send LoginForm1
        .send "JSON=textLogin"
End With

回答1:


Try this code

Public Declare Function InternetSetOptionStr Lib "wininet.dll" Alias "InternetSetOptionA" (ByVal hInternet As Long, ByVal lOption As Long, ByVal sBuffer As String, ByVal lBufferLength As Long) As Integer

Private Sub ClearCacheHistory()
    InternetSetOptionStr 0, 42, 0, 0
End Sub

Sub GPRO_Login()
    Dim html As MSHTML.HTMLDocument, sUser As String, sPass As String
    ClearCacheHistory
    Set html = New MSHTML.HTMLDocument
    sUser = "youremail": sPass = "yourpass"
    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", "https://gpro.net/gb/Login.asp?langCode=gb&langCode=gb&Redirect=gpro.asp", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send "textLogin=" & sUser & "&textPassword=" & sPass & "&Logon=Login"
        html.body.innerHTML = .responseText
        If html.querySelector("ul[id='nav'] li a[href*='/gb/Login']").innerText = "Logout" Then
            MsgBox "Login Success", vbInformation
        Else
            MsgBox "Login Failure", vbExclamation
        End If
    End With
End Sub


来源:https://stackoverflow.com/questions/62465470/excel-vba-msxml2-xmlhttp-unable-to-login-to-website-with-form-method-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!