• curl获得cookie数据<转>


      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
    ", 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

  • 相关阅读:
    VOC2012数据集
    flask 图像接口测试
    PyQt5 学习指南
    超分辨中的注意力机制:Attention in Super Resolution
    OpenCV:图像增亮与直方图均衡
    SOA 架构与微服务架构的区别
    Docker 部署 vue 项目
    element ui 中的 resetFields() 报错'resetFields' of undefined
    Mybatis分页方法
    Serializable的作用
  • 原文地址:https://www.cnblogs.com/wainiwann/p/11062878.html
Copyright © 2020-2023  润新知