如何使用 libcurl 登录本网站?(C++)
How do I use libcurl to log into this website? (C++)
我想制作一个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" />
你的代码有两个问题:
-
您的
data
未完全正确进行网址编码。 您可以(并且应该)使用curl_easy_escape()
正确编码您发布的每个name
和value
name=value
对的每个组件CURLOPT_POSTFIELDS
. -
您使用
CURLOPT_POSTFIELDS
发布data
,它以application/x-www-form-urlencoded
格式发布(这是许多在线网络表单的默认设置),但有问题的特定网络表单实际上希望data
以multipart/form-data
格式发布(由于网络表单的enctype="multipart/form-data"
属性),因此您需要改用CURLOPT_HTTPPOST
(已弃用)或CURLOPT_MIMEPOST
。
尝试更多类似的东西:
#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;
}
或者这个:
#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
curl_mime *mime = curl_mime_init(myHandle);
curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "ctl00$LoginTextBox");
curl_mime_data(part, "USERNAME", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "ctl00$PasswordTextbox");
curl_mime_data(part, "PASSWORD", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "ctl00$LogInButton");
curl_mime_data(part, "Sign in", CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "ctl00$HiddenValue");
curl_mime_data(part, "Initial Value", CURL_ZERO_TERMINATED);
curl_easy_setopt(myHandle, CURLOPT_MIMEPOST, data);
curl_easy_perform(myHandle);
curl_mime_free(mime);
curl_easy_cleanup(myHandle);
return 0;
}
相关文章:
- 如何使用url确定网站协议
- 如何从网站获取数据并将其传输到数据库?
- 网站和C++程序在同一台 Linux PC 上的通信
- 从网站复制证书,并使用脚本将其添加到受信任的根证书颁发机构
- 谷歌浏览器在访问网站时发送第二个 0 长度的请求?
- 为什么作业提交网站的输出与Visual Studio不同
- 如何使用 libcurl 登录本网站?(C++)
- 访问其他网站后如何获得完整(精确)的URL / QUrl地址?
- 网站组装 |错误:'C/ObjC'不允许使用无效'-std=c++11'参数
- C :如何将多个URL从网站HTML提取到向量
- Euler网站第八次工作
- PHP 网站在传递图像作为参数时未运行 exec 函数
- C++应用程序连接到网站服务器并根据密码检索文件
- C++ getline(cin,变量)行为不端.网站上没有任何解决方案有帮助
- 如何检查网站是否包含c++中的字符串
- 如何在我的网站上使用在线C++编译器
- 如何使用QDNSlookup类找到网站的IP地址
- 使用winsock2通过Socks5下载HTTP网站
- 在网站上发布 Win32 项目
- 创建一个 C++ 类以使用 libcurl 将数据从网站读取到字符串