如何使用表格(method = post)登录网站? [英] How to login to website with form (method=post)?

查看:64
本文介绍了如何使用表格(method = post)登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码来刮擦需要登录的网站的HTML.最初,我使用IE自动化,但速度较慢,因此我正在探索其他选项,包括MSXML2.XMLHTTP.

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

我可以在没有IE的任何问题的情况下登录网站,但是不能使其与MSXML2.XMLHTTP一起使用.

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

这有效:

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

无法登录:

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

这是目标网站的HTML

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>

我决定改用WinHTTPRequest.下面的代码有效.

I decided to use WinHTTPRequest instead. Code below works.

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

推荐答案

尝试此代码

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

这篇关于如何使用表格(method = post)登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆