Invoke-WebRequst登录网站

时间:2017-07-28 07:45:41

标签: powershell web login

我想使用命令行工具登录网站http://url/login.html 此页面没有表单但有三个输入字段

        <div class="login-form">
        <div class="phone-number">
            <input id="account" type="text" placeholder="Account" />
        </div>
        <div class="code-input" style="position: relative;">
            <input id="acc_pass" type="password" placeholder="Password" style="height:35px;" />
        </div>
        <p id="js_acc_error" style="color:red;margin-top:10px;"></p>
        <div class="login-other">
            <input id="account_login_checkbox" type="checkbox" /> <span>Read and agree<a class="show-xieyi" style="color:#3a74ba;cursor:pointer;"> EUL </a></span>
        </div>
        <div style="display:none; margin:4px 0 0 0;">
        </div>
        <div class="login-btn" style="margin:10px 0 0">
            <button id="account_login_btn" type="submit">Login</button>
        </div>
    </div>

我使用Fiddler捕获请求后点击提交按钮和重播请求使用curl但有时因为cookie改变而无法工作。

所以我想使用命令填充输入字段并单击按钮来重播请求。如何填写字段并单击Powershell Invoke-WebRequest或其他命令行工具中的按钮?

感谢。

2 个答案:

答案 0 :(得分:0)

当您单击登录按钮时,它可能只是向该地址发送一些 HTTP 请求,但使用 POST 方法。在powershell中,您可以执行以下操作:

curl 'https://site/longin.html' -Body "account=john&acc_pass=1234&account_login_checkbox=false" -Method Post

您可能需要稍微调整一下,但它应该有效。另外, curl 只是powershell的 Invoke-WebRequest 的别名。

答案 1 :(得分:0)

由于curl无法保存cookie,所以我发现使用powershell调用ie并模拟为手动输入

  1. 如果使用Windows 10和IE 11,请配置为此链接 https://blogs.msdn.microsoft.com/ieinternals/2011/08/03/default-integrity-level-and-automation/

  2. 兼容性视图设置中的取消复选框

  3. 像这样使用powershell

    $url = "http://web.com/login.html"
    $username="username"
    $password="password"
    $ie = new-object -com "InternetExplorer.ApplicationMedium"
    $ie.visible=$false
    $ie.navigate("$url")
    while($ie.ReadyState -ne 4) {start-sleep -m 100}
    $ie.Document.IHTMLDocument3_getElementById("account").value = $username
    $ie.Document.IHTMLDocument3_getElementById("acc_pass").value = $password
    $ie.Document.IHTMLDocument3_getElementById("account_login_checkbox").checked=$true
    $ie.Document.IHTMLDocument3_getElementById("account_login_btn").click()
    start-sleep -m 1000
    $ie.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
    Remove-Variable ie
    
相关问题