Cookies not being saved in cURL C++, such a simple and clean code example won't work

折月煮酒 提交于 2019-12-12 02:33:17

问题


why isn't the cookies storing? the local website I made has a setcookie("test",time()); in PHP and it works in the browser normally, but in cURL it just won't work, I don't even know what to try anymore, this is the simplest I got by trimming the code, and still don't save the cookies in a file. it doesn't even create the file to save it (btw the windows has the permissions to add any file in the root since I'm moving and creating files all the time there), and Yes the site example set cookies. what is wrong with it ?

The content output is correct and it should work. but it just doesn't work. :/

thanks in advance.

#include <curl/curl.h>
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
            ((std::string*)userp)->append((char*)contents, size * nmemb);
            return size * nmemb;
        }

int main () {
        CURL *curl2;
        CURLcode res;




        curl_global_init(CURL_GLOBAL_ALL);



       string url2 = "http://localhost/test.php";
       url2 = "http://www.facebook.com";

        curl2 = curl_easy_init();
        if(curl2) {

            string html2 = "";
            struct curl_slist *chunk = NULL;


        curl_easy_setopt(curl2, CURLOPT_URL, url2.c_str());     
        curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "C:\\teste.txt");
        curl_easy_setopt(curl2, CURLOPT_COOKIEJAR, "C:\\teste.txt");                
        curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl2, CURLOPT_WRITEDATA, &html2);


        res = curl_easy_perform(curl2);

        std::cout << "RESULT: " << html2;

        }


        _getch();
        return 0;
}

回答1:


Remember to call curl_easy_cleanup(curl2)



来源:https://stackoverflow.com/questions/9884048/cookies-not-being-saved-in-curl-c-such-a-simple-and-clean-code-example-wont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!