如何使用libcurl登录该网站? (C ++)

时间:2018-07-09 19:46:42

标签: c++ curl login web libcurl

我想制作一个C ++程序,以使用登录凭据(目前我将其硬编码到用户名和密码字段中)填充HTML网络表单,然后提交该网络表单并转到下一个HTML页面。

使用下面的代码,它一直卡在登录页面上并且无法向前移动。在控制台中,我不断获得登录页面的HTML源代码,而不是登录后出现的页面。

有人可以逐步指导我吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main()
{
    curl_global_init( CURL_GLOBAL_ALL );
    CURL * myHandle = curl_easy_init ( );

    // Set up a couple initial paramaters that we will not need to mofiy later.
    curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/5.0");
    curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1 );
    curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1 );
    curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");

    // Visit the login page once to obtain a PHPSESSID cookie
    curl_easy_setopt(myHandle, CURLOPT_URL, "website login page URL here");
    curl_easy_perform( myHandle );

    // Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
    curl_easy_setopt(myHandle, CURLOPT_REFERER, "website login page URL here");
    // Next we tell LibCurl what HTTP POST data to submit
    char *data="ctl00$LoginTextBox=USERNAME&ctl00$PasswordTextbox=PASSWORD&ctl00$LogInButton=Sign in&ctl00$HiddenValue=Initial Value";
    curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, data);
    curl_easy_perform( myHandle );
    curl_easy_cleanup( myHandle );
    return 0;
}

这是我正在处理的HTML网络表单:

<div id="loginForm">
    <div id="loginInput">
        <label for="ctl00_LoginTextBox" id="ctl00_LoginLabel">Name/Badge</label>
        <input name="ctl00$LoginTextBox" type="text" id="ctl00_LoginTextBox" class="PopupInput" />
    </div>
    <div id="passwordInput">
        <label for="ctl00_PasswordTextbox" id="ctl00_PasswordLabel">Password</label>
        <input name="ctl00$PasswordTextbox" type="password" id="ctl00_PasswordTextbox" class="PopupInput" />        
    </div>
    <div id="equipmentbox">

    </div>
</div>

<div id="ctl00_LoginButtons">
    <input type="submit" name="ctl00$LogInButton" value="Sign in" id="ctl00_LogInButton" class="btn" />
    <input type="submit" name="ctl00$SwitchLoginScreenModeBT" value="Change Password" id="ctl00_SwitchLoginScreenModeBT" class="btn" /> 
</div>

<input name="ctl00$HiddenValue" type="hidden" id="ctl00_HiddenValue" value="Initial Value" />
<input name="ctl00$HiddenValue2" type="hidden" id="ctl00_HiddenValue2" value="Initial Value" />

1 个答案:

答案 0 :(得分:3)

您的代码有两个问题:

  • 您的await Task.WhenAll(appRoleMaps.Select(async x => x.AuthorizedUsers = await GetAuthorizedUsers(x.DomainName, x.ADGroupName))); 未正确进行完整的url编码。您可以(并且应该)使用curl_easy_escape()对用data发布的每个name对中的每个valuename=value组件进行正确编码。

  • 您正在使用CURLOPT_POSTFIELDS发布data,它以CURLOPT_POSTFIELDS格式发布(这是许多在线Webforms的默认设置),但是实际正在使用特定的Webform application/x-www-form-urlencoded改为以data格式发布(由于Web表单的multipart/form-data属性),因此您需要改用CURLOPT_HTTPPOST(不推荐使用)或CURLOPT_MIMEPOST

尝试更多类似的方法:

enctype="multipart/form-data"

或者这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main()
{
    curl_global_init(CURL_GLOBAL_ALL);
    CURL *myHandle = curl_easy_init();

    // Set up a couple initial paramaters that we will not need to mofiy later.
    curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/5.0");
    curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1);
    curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");

    // Visit the login page once to obtain a PHPSESSID cookie
    curl_easy_setopt(myHandle, CURLOPT_URL, "website login page URL here");
    curl_easy_perform(myHandle);

    // Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
    curl_easy_setopt(myHandle, CURLOPT_REFERER, "website login page URL here");

    // Next we tell LibCurl what HTTP POST data to submit
    struct curl_httppost *data = NULL;
    struct curl_httppost *last = NULL;

    curl_formadd(&data, &last, CURLFORM_PTRNAME, "ctl00$LoginTextBox", CURLFORM_PTRCONTENTS, "USERNAME", CURLFORM_END);
    curl_formadd(&data, &last, CURLFORM_PTRNAME, "ctl00$PasswordTextbox", CURLFORM_PTRCONTENTS, "PASSWORD", CURLFORM_END);
    curl_formadd(&data, &last, CURLFORM_PTRNAME, "ctl00$LogInButton", CURLFORM_PTRCONTENTS, "Sign in", CURLFORM_END);
    curl_formadd(&data, &last, CURLFORM_PTRNAME, "ctl00$HiddenValue", CURLFORM_PTRCONTENTS, "Initial Value", CURLFORM_END);
    curl_easy_setopt(myHandle, CURLOPT_HTTPPOST, data);

    curl_easy_perform(myHandle);

    curl_formfree(data);
    curl_easy_cleanup(myHandle);

    return 0;
}