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

posted on 2019-06-21 09:57  瓦楞球  阅读(3671)  评论(0编辑  收藏  举报