CURL *curl; CURLcode res; struct curl_slist *headers = NULL; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { //初始化cookie引擎 curl_easy_setopt(curl,CURLOPT_COOKIEFILE,""); //初始化cookie引擎,才能正確接收到cookie數據. curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_URL,"https://passport.csdn.net/account/login"); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt"); //把服務器發過來的cookie保存到cookie_open.txt curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //FILE *bodyfile; //bodyfile = fopen("open.html","w"); //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //寫數據的回調函數存文件 //curl_easy_setopt(curl,CURLOPT_WRITEDATA, bodyfile); string content; //設置回調函數 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); //執行http請求 res = curl_easy_perform(curl); //如果執行成功, if(res == CURLE_OK) { struct curl_slist *cookies = NULL; curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&cookies); //獲得cookie數據 int i=1; while (cookies) { TRACE("[%d]: %s\n", i, cookies->data); cookies = cookies->next; i++; } } //再次請求的地址 char *token_url="https://passport.csdn.net/account/login"; //釋放資源 //fclose(bodyfile); curl_slist_free_all(headers); curl_easy_cleanup(curl); } curl_global_cleanup();
源地址:https://blog.csdn.net/qing666888/article/details/43623431