【问题标题】:CURLOPT_COOKIEJAR and cookie fileCURLOPT_COOKIEJAR 和 cookie 文件
【发布时间】:2015-05-15 13:24:50
【问题描述】:

我正在使用 CURLOPT_COOKIEJAR(卷曲库),并将 cookie 保存在文件“cookie.txt”中。我找不到它的文件。它在哪里?我在项目文件夹中搜索,没有。

这是我的代码:

curl_easy_setopt(handle,CURLOPT_COOKIESESSION,1);       // clean up session.
curl_easy_setopt(handle,CURLOPT_COOKIEJAR,"cookie.txt");
curl_easy_setopt(handle,CURLOPT_USERAGENT,userAgentMozilla);        // set the user agent
curl_easy_setopt(handle,CURLOPT_URL,lien.toUtf8().constData());     // set the url 
curl_easy_setopt(handle,CURLOPT_POSTFIELDSIZE,data.size()+4);     // size of the request.
curl_easy_setopt(handle,CURLOPT_POSTFIELDS,ba);  // aray of the POST request
res = curl_easy_perform(handle);  

【问题讨论】:

  • 一定要放绝对路径吗?

标签: curl cookies libcurl


【解决方案1】:

当然你必须这样做。

你可以试试这个代码:

CURLOPT_COOKIEJAR=>__DIR__."/cookie.txt",

CURLOPT_COOKIEJAR=>dirname(__FILE__)."/cookie.txt",

【讨论】:

  • 不需要绝对路径。看我的回答。
【解决方案2】:

不需要绝对路径。 正在清理 cookie 会话的 CURLOPT_COOKIESESSION 似乎与 CURLOPT_COOKIEJAR 冲突。这不会清理 cookie 会话、处理请求然后将新的 cookie 保存到您的文件中。但是,这将阻止在此请求中创建新的 cookie 会话。 正确的代码很简单:

curl_easy_setopt(handle,CURLOPT_COOKIEJAR,"cookie.txt");
curl_easy_setopt(handle,CURLOPT_USERAGENT,userAgentMozilla);        // set the user agent
curl_easy_setopt(handle,CURLOPT_URL,lien.toUtf8().constData());     // set the url 
curl_easy_setopt(handle,CURLOPT_POSTFIELDSIZE,data.size()+4);     // size of the request.
curl_easy_setopt(handle,CURLOPT_POSTFIELDS,ba);  // aray of the POST request
res = curl_easy_perform(handle);

【讨论】:

    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2014-02-28
    • 2013-03-17
    相关资源
    最近更新 更多