如何将此 curl 命令转换为 libcurl
How to convert this curl command to libcurl
我使用这样的 curl 命令成功登录 www.tistory.com。
curl -c cookie.txt -d "loginId=xxx&password=xxx&form_id=authForm" https://www.tistory.com/auth/login
并且...为了在我的 C++ 项目中使用它,我将它转换为 libcurl,就像这样。
CURL *curl;
CURLcode res;
char *location;
long response_code;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://www.tistory.com/auth/login");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
const char* postData = "loginId=xxx&password=xxx&form_id=authForm";
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postData));
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
......
}
......
此转换后的代码无效。 cookie.txt 文件也没有保存。我不知道哪里错了或者我错过了什么。有人告诉我。提前致谢。
这是结果。
enter image description here
将 curl 命令行转换为 C 代码的最佳方法是使用 --libcurl code.c
命令行选项。
这样做并将输出与您的代码片段进行比较时,这两个程序之间的唯一区别似乎是您的 libcurl-using 程序中缺少 User-Agent:
header .
我使用这样的 curl 命令成功登录 www.tistory.com。
curl -c cookie.txt -d "loginId=xxx&password=xxx&form_id=authForm" https://www.tistory.com/auth/login
并且...为了在我的 C++ 项目中使用它,我将它转换为 libcurl,就像这样。
CURL *curl;
CURLcode res;
char *location;
long response_code;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://www.tistory.com/auth/login");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
const char* postData = "loginId=xxx&password=xxx&form_id=authForm";
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postData));
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
......
}
......
此转换后的代码无效。 cookie.txt 文件也没有保存。我不知道哪里错了或者我错过了什么。有人告诉我。提前致谢。
这是结果。
enter image description here
将 curl 命令行转换为 C 代码的最佳方法是使用 --libcurl code.c
命令行选项。
这样做并将输出与您的代码片段进行比较时,这两个程序之间的唯一区别似乎是您的 libcurl-using 程序中缺少 User-Agent:
header .