Libcurl C++ - 我如何 post 为这个特定的表格形成数据?
Libcurl C++ - How do I post form data for this specific form?
如何使用 libcurl 和 C++ post 然后提交以下表单的表单数据?
<form id="loginContainer" class="controllerContainer" action="/j_spring_security_check" method="post">
<h2>Login</h2>
<hr>
<fieldset>
<div class="field">
<input class="standard" name="j_username" autocomplete="off" autocapitalize="off" size="25" placeholder="Enter Username">
</div>
<div class="field">
<input class="standard" name="j_password" type="password" value="" size="25" autocapitalize="off" autocomplete="off" placeholder="Enter Password (case-sensitive)">
</div>
<div class="field">
<button class="login" type="submit"></button>
</div>
</fieldset>
我尝试了 curl 网站上的几个例子,但 none 对我有用。
编辑:这是我用来登录的函数
void loginToWebsite(std::string url, std::string userAgent, std::string username, std::string password)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ("j_username=" + username + "&j_password=" + password).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
res = curl_easy_perform(curl);
}
您正在泄露 curl
对象。使用完毕后需要调用curl_easy_cleanup()
。
但是,更重要的是,您使用的 CURLOPT_POSTFIELDS
不正确。您正在向它传递一个 char*
指向临时 std::string
的指针,该指针立即超出范围,在调用 curl_easy_perform()
时留下 curl 指向无效内存的悬空指针.您需要
使用本地 std::string
变量
您还需要对 post 的表单参数进行 url 编码。您可以为此使用 curl_easy_escape()
。
话虽如此,试试这样的东西:
#include <string>
#include <sstream>
void loginToWebsite(const std::string &url, const std::string &userAgent, const std::string &username, const std::string &password)
{
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
std::ostringstream oss;
oss << "j_username=";
char *encoded = curl_easy_escape(curl, username.c_str(), username.length());
if (encoded)
{
oss << encoded;
curl_free(encoded);
}
oss << "&j_password=";
encoded = curl_easy_escape(curl, password.c_str(), password.length());
if (encoded)
{
oss << encoded;
curl_free(encoded);
}
std::string postdata = oss.str();
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
CURLcode res = curl_easy_perform(curl);
// use res as needed...
curl_easy_cleanup(curl);
}
}
如何使用 libcurl 和 C++ post 然后提交以下表单的表单数据?
<form id="loginContainer" class="controllerContainer" action="/j_spring_security_check" method="post">
<h2>Login</h2>
<hr>
<fieldset>
<div class="field">
<input class="standard" name="j_username" autocomplete="off" autocapitalize="off" size="25" placeholder="Enter Username">
</div>
<div class="field">
<input class="standard" name="j_password" type="password" value="" size="25" autocapitalize="off" autocomplete="off" placeholder="Enter Password (case-sensitive)">
</div>
<div class="field">
<button class="login" type="submit"></button>
</div>
</fieldset>
我尝试了 curl 网站上的几个例子,但 none 对我有用。
编辑:这是我用来登录的函数
void loginToWebsite(std::string url, std::string userAgent, std::string username, std::string password)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ("j_username=" + username + "&j_password=" + password).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
res = curl_easy_perform(curl);
}
您正在泄露 curl
对象。使用完毕后需要调用curl_easy_cleanup()
。
但是,更重要的是,您使用的 CURLOPT_POSTFIELDS
不正确。您正在向它传递一个 char*
指向临时 std::string
的指针,该指针立即超出范围,在调用 curl_easy_perform()
时留下 curl 指向无效内存的悬空指针.您需要
使用本地
std::string
变量
您还需要对 post 的表单参数进行 url 编码。您可以为此使用 curl_easy_escape()
。
话虽如此,试试这样的东西:
#include <string>
#include <sstream>
void loginToWebsite(const std::string &url, const std::string &userAgent, const std::string &username, const std::string &password)
{
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
std::ostringstream oss;
oss << "j_username=";
char *encoded = curl_easy_escape(curl, username.c_str(), username.length());
if (encoded)
{
oss << encoded;
curl_free(encoded);
}
oss << "&j_password=";
encoded = curl_easy_escape(curl, password.c_str(), password.length());
if (encoded)
{
oss << encoded;
curl_free(encoded);
}
std::string postdata = oss.str();
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
CURLcode res = curl_easy_perform(curl);
// use res as needed...
curl_easy_cleanup(curl);
}
}