【发布时间】:2018-07-31 09:00:07
【问题描述】:
我通过这样的 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 文件也没有保存。我不知道哪里错了,或者我错过了什么。有人告诉我。提前致谢。
这是结果。
【问题讨论】:
-
C 还是 C++?它们是两种截然不同的语言,所以请选择一种。 如何代码不起作用?请read about how to ask good questions,学习如何创建Minimal, Complete, and Verifiable Example,也请阅读this question checklist。
-
注意你有
cookies.txt而不是cookie.txt -
哦,对不起。我选择了 c++ 并添加了结果图像以显示响应代码和编辑的错字。
-
看来唯一的区别是在您的 libcurl 案例中缺少
User-Agent:标头...(请不要使用图像来显示文本响应) -
@DanielStenberg 你是对的。我插入了这段代码
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.61.0");,它工作正常。非常感谢你!我会记住你关于文本回复的建议。 :)