【问题标题】:How to convert this curl command to libcurl如何将此 curl 命令转换为 libcurl
【发布时间】: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 文件也没有保存。我不知道哪里错了,或者我错过了什么。有人告诉我。提前致谢。

这是结果。

enter image description here

【问题讨论】:

  • 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");,它工作正常。非常感谢你!我会记住你关于文本回复的建议。 :)

标签: c++ curl libcurl


【解决方案1】:

将 curl 命令行转换为 C 代码的最佳方法是使用 --libcurl code.c 命令行选项。

当这样做并将输出与您的代码 sn-p 进行比较时,两个程序之间的唯一区别似乎是您的 libcurl-using 程序中缺少 User-Agent: 标头。

【讨论】:

  • 我正在寻找一种将 curl 转换为 libcurl 的方法,但没有想到 --libcurl 选项,谢谢
  • 像魅力一样工作!顺便说一句,我尝试过手动转换、邮递员和其他一些在线转换。全部失败。
猜你喜欢
  • 2021-10-08
  • 2012-05-10
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
相关资源
最近更新 更多