• c++ curl 登陆renren.com (cookie的使用)<转>


    size_t write_callback( void *ptr, size_t size, size_t nmemb, void *stream )
    {
        int len = size * nmemb;
        int written = len;
    
        if ( access( (char*)stream, 0 ) == -1 )
        {
            fp = fopen( (char*) stream, "wb" );
        }
        else
        {
            fp = fopen( (char*) stream, "ab" );
        }
        if (fp)
        {
            fwrite( ptr, size, nmemb, fp );
        }
        return written;
    }
    
    
    int PostUrlchar* url, void *savepath)
    {
        // 初始化libcurl
        CURLcode return_code;
        return_code = curl_global_init(CURL_GLOBAL_WIN32);
        if (CURLE_OK != return_code)
            return 0;
        CURL *curl_handle;
        CURLcode res;
        curl_handle = curl_easy_init();
        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);
        curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"); 
        curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookie.txt"); 
        curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookie.txt"); 
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, savepath );
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS,postLoginData);
        res = curl_easy_perform(curl_handle); 
        curl_easy_cleanup(curl_handle);
        curl_global_cleanup();
        if (res == CURLE_OK)
        {
            return 1;
        }
        return 0;
    }
    
    int GetUrl(string url,void *buffer)
    {
        // 初始化libcurl
        CURLcode return_code;
        return_code = curl_global_init(CURL_GLOBAL_WIN32);
        if (CURLE_OK != return_code)
            return 0;
        CURL *easy_handle = curl_easy_init();
        if (NULL == easy_handle)
        {  
            curl_global_cleanup();
            return 0;
        }
        // 设置easy handle属性
        curl_easy_setopt(easy_handle, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"); 
        curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION,TRUE); 
        curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
        curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
        curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"cookie.txt");
        curl_easy_setopt(easy_handle, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_easy_perform(easy_handle); 
        curl_easy_cleanup(easy_handle);
        curl_global_cleanup();
        return 1;
    }
    1.先post登陆到url。
    
    2.再访问自己的主页。
    
    可以使用Wireshark抓取各个web地址,及post具体格式的数据。

    原帖地址:https://www.xuebuyuan.com/725671.html

  • 相关阅读:
    数据库学习摘记 —— 关系代数和关系演算
    数据库学习摘记 —— 数据库基本概念杂记
    POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
    POJ 3311 Hie with the Pie (状压dp)
    hdu 1533 Going Home (最小费用最大流)
    bzoj 2115 Xor (线性基)
    hdu 5120 Intersection (圆环面积相交->圆面积相交)
    BZOJ 2460 元素(线性基)
    POJ 3348 Cows (凸包模板+凸包面积)
    UVA 12012 Detection of Extraterrestrial(KMP求循环节)
  • 原文地址:https://www.cnblogs.com/wainiwann/p/10950358.html
Copyright © 2020-2023  润新知