VC使用libcurl模拟登录CSDN并自动评论资源以获取积分

环境:Win7 64位+VC2008

软件及源码下载:(http://pan.baidu.com/s/1jGE52pK)

涉及到的知识点:

C++多线程编程

libcurl的使用(包括发送http请求、发送cookie给服务器、保存cookie)

关于libcurl的资料,推荐大家参考下官方文档:http://curl.haxx.se/libcurl/c/example.html

软件运行结果

libcurl中的所有函数

curl_easy_duphandle 
curl_easy_escape 
curl_easy_getinfo 
curl_easy_init 
curl_easy_pause 
curl_easy_perform 
curl_easy_recv 
curl_easy_reset 
curl_easy_send 
curl_easy_setopt 
curl_easy_strerror 
curl_easy_unescape 
curl_escape (deprecated, do not use
curl_formadd 
curl_formfree 
curl_formget 
curl_free 
curl_getdate 
curl_getenv (deprecated, do not use
curl_global_cleanup 
curl_global_init 
curl_global_init_mem 
curl_mprintf (deprecated, do not use
curl_multi_add_handle 
curl_multi_assign 
curl_multi_cleanup 
curl_multi_fdset 
curl_multi_info_read 
curl_multi_init 
curl_multi_perform 
curl_multi_remove_handle 
curl_multi_setopt 
curl_multi_socket 
curl_multi_socket_action 
curl_multi_strerror 
curl_multi_timeout 
curl_share_cleanup 
curl_share_init 
curl_share_setopt 
curl_share_strerror 
curl_slist_append 
curl_slist_free_all 
curl_strequal (deprecated, do not use
curl_strnequal (deprecated, do not use
curl_unescape (deprecated, do not use
curl_version 
curl_version_info

 
以下几个函数需要重点掌握

CURLcode curl_global_init(long flags)
描述:这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用)
如果这个函数在curl_easy_init函数调用时还没调用,它将由libcurl库自动完成。
参数:flags
CURL_GLOBAL_ALL           //初始化所有的可能的调用。
CURL_GLOBAL_SSL           //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32         //初始化win32套接字库。
CURL_GLOBAL_NOTHING     //没有额外的初始化。
 
void curl_global_cleanup(void);
描述:在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数。
 

char *curl_version( );

描述: 打印当前libcurl库的版本
 
CURL *curl_easy_init( );
描述:curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理.
一般curl_easy_init意味着一个会话的开始. 它的返回值一般都用在easy系列的函数中
 
void curl_easy_cleanup(CURL *handle);
描述:这个调用用来结束一个会话.与curl_easy_init配合着用. 
参数:CURL类型的指针.
 
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.
它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.
(这个函数有些像ioctl函数)
参数:
1 CURL类型的指针
2 各种CURLoption类型的选项.(都在curl.h库里有定义,man 也可以查看到)
3 parameter 这个参数 既可以是个函数的指针,也可以是某个对象的指针,也可以是个long型的变量.它用什么这取决于第二个参数.
CURLoption 这个参数的取值很多.具体的可以查看man手册.
 
CURLcode curl_easy_perform(CURL *handle);
描述:这个函数在初始化CURL类型的指针 以及curl_easy_setopt完成后调用. 就像字面的意思所说perform就像是个舞台.让我们设置的
option 运作起来.
参数:
CURL类型的指针 

如何操作cookie

通过curl_easy_setopt函数的第二个参数,就可以很容易的操作cookie
保存cookie信息到本地的cookie.txt文件
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");

读取本地的cookie.txt文件中的cookie信息

1
curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE, "cookie.txt");

模拟登录csdn

复制代码
ReturnInfo CCSDNDlg::LoginServer(CString strUser,CString strPass)
{
    ReturnInfo returnInfo;
    returnInfo.bReturn = FALSE;

    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, "");
        curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1L);

        //http请求头
        headers = curl_slist_append(headers,"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");    //模拟浏览器
        headers = curl_slist_append(headers,"Host:passport.csdn.net");
        headers = curl_slist_append(headers,"Accept:*/*");
        headers = curl_slist_append(headers,"Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        headers = curl_slist_append(headers,"Accept-Encoding:gzip, deflate");
        headers = curl_slist_append(headers,"X-Requested-With:XMLHttpRequest");
        headers = curl_slist_append(headers,"Referer:https://passport.csdn.net/account/loginbox?callback=logined&hidethird=1&from=http%3a%2f%2fwww.csdn.net%2f");
        headers = curl_slist_append(headers,"Connection:keep-alive");
        
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");        //把服务器发过来的cookie保存到cookie.txt

        //发送http请求头
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        char url[256];
        sprintf(url,"http://passport.csdn.net/ajax/accounthandler.ashx?t=log&u=%s&p=%s&remember=0&f=http%3A%2F%2Fwww.csdn.net%2F&rand=0.47590136872096434",strUser,strPass);
        curl_easy_setopt(curl, CURLOPT_URL,url);

        string content;
        //设置回调函数
        res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);

        //执行http请求
        res = curl_easy_perform(curl);

        string returnVal;
        Utf8ToMb((char*)content.c_str(),content.length(),returnVal);
        int pos = returnVal.find(""status":true");
        if ( pos >= 0){
            returnInfo.bReturn = TRUE;
            int nStartPos = content.find("data":");
            int nEndPos      = content.rfind(""}}");
            returnInfo.data = content.substr(nStartPos+6,nEndPos - nStartPos-4);
        }else{
            int nStartPos = returnVal.find("error":");
            int nEndPos      = returnVal.find("data":",nStartPos);
            returnInfo.strErrorInfo = returnVal.substr(nStartPos+8,nEndPos-nStartPos-11);
        }
        //释放资源
        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
        headers = NULL;
    }
    curl_global_cleanup();
    return returnInfo;
}
复制代码

根据给定的网址获取网页源码

复制代码
static string GetHtmlPage(string url)
{
    CURL *easy_handle;
    CURLcode res;
    string content;
    curl_global_init(CURL_GLOBAL_ALL);
    easy_handle = curl_easy_init();
    if( easy_handle )
    {
        //初始化cookie引擎
        curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"");
        curl_easy_setopt(easy_handle,CURLOPT_TIMEOUT,5);            //设置请求超时时间
        //curl_easy_setopt(easy_handle,CURLOPT_VERBOSE,1);            //输出请求头和响应头
        //curl_easy_setopt(easy_handle,CURLOPT_HEADER,1);
        curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION, 1L);

        //http请求头
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers,"Host: download.csdn.net");
        headers = curl_slist_append(headers,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
        headers = curl_slist_append(headers,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        headers = curl_slist_append(headers,"Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");        
        headers = curl_slist_append(headers,"Referer: http://www.csdn.net/");
        headers = curl_slist_append(headers,"Connection: keep-alive");
        curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE, "cookie.txt");        //读取本地存储的cookie

        if(!InitCurl(easy_handle,res,url,content))
        {
            //释放资源
            curl_slist_free_all(headers);
            curl_easy_cleanup(easy_handle);
            return NULL;
        }
        //释放资源
        curl_slist_free_all(headers);
        curl_easy_cleanup(easy_handle);
    }
    curl_global_cleanup();
    string tt;
    Utf8ToMb((char *)content.c_str(),content.length(),tt);
    return tt;
}
复制代码

获取下载资源总页数

复制代码
static int GetTotalPageNum()
{
    string url = "http://download.csdn.net/my/downloads/1";

    string html = GetHtmlPage(url);
    int nPos = html.rfind("尾页");
    if (nPos == -1)
        return -1;
    nPos -= 2;
    int nStartPos = html.rfind("/",nPos);
    string strTotal = html.substr(nStartPos+1,nPos - nStartPos - 1);
    return atoi(strTotal.c_str());
}
复制代码

获取待评论的资源列表

复制代码
static vector<DownResourceInfo> GetToCommentList(int pageNum)
{
    vector<DownResourceInfo> vtDownload;
    char url[128] = {0};
    sprintf(url,"http://download.csdn.net/my/downloads/%d",pageNum);
    string html = GetHtmlPage(url);
    int nPos = 0;
    int n = 0;
    int flag = 1;
    while((nPos = html.find("#comment",n)) != -1)
    {
        n = nPos+1;
        int nStartPos = html.rfind("/",nPos);
        string strUrl = html.substr(nStartPos+1,nPos - nStartPos -1);
        DownResourceInfo info;
        info.strResourceCurl = strUrl;
        //获取资源的名字
        nStartPos = html.find(strUrl,nPos+1);
        if(nStartPos == -1)
            return vtDownload;
        nStartPos += 2;
        nStartPos += strUrl.length();
        int nEndPos = html.find("</a>",nStartPos);
        string ResourceName = html.substr(nStartPos,nEndPos - nStartPos);
        info.strResourceName = ResourceName;        
        vtDownload.push_back(info);
    }
    return vtDownload;
}
复制代码

发表评论

复制代码
static BOOL AddComment(string sourceId)
{
    CURL *easy_handle;
    CURLcode res;
    string content;
    curl_global_init(CURL_GLOBAL_ALL);
    easy_handle = curl_easy_init();
    if( easy_handle )
    {
        //初始化cookie引擎
        curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"");        
        curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION, 1L);        
        string url = "http://download.csdn.net/index.php/comment/post_comment?jsonpcallback=jsonp1385304626524&sourceid="+sourceId+"&content=%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%B0%A2%EF%BC%8C%E8%BF%99%E8%B5%84%E6%BA%90%E6%88%91%E6%89%BE%E4%BA%86%E5%A5%BD%E4%B9%85%E4%BA%86%EF%BC%81&rating=5&t=1385304679900";
        string referer = "Referer: http://download.csdn.net/detail/wasdzxce/"+sourceId;
        //http请求头
        struct curl_slist *headers = NULL;        
        headers = curl_slist_append(headers,"Host: download.csdn.net");
        headers = curl_slist_append(headers,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
        headers = curl_slist_append(headers,"Accept: text/javascript, application/javascript, */*");
        headers = curl_slist_append(headers,"Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        headers = curl_slist_append(headers,"Accept-Encoding: gzip, deflate");
        headers = curl_slist_append(headers,"Content-Type: application/x-www-form-urlencoded");
        headers = curl_slist_append(headers,"X-Requested-With: XMLHttpRequest");
        headers = curl_slist_append(headers,referer.c_str());
        headers = curl_slist_append(headers,"Connection: keep-alive");
        curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE, "cookie.txt");        //读取本地存储的cookie 

        if(!InitCurl(easy_handle,res,url,content))
        {
            //释放资源
            curl_slist_free_all(headers);
            curl_easy_cleanup(easy_handle);
            curl_global_cleanup();
            return FALSE;
        }
        //释放资源
        curl_slist_free_all(headers);
        curl_easy_cleanup(easy_handle);
    }
    curl_global_cleanup();
    int pos = content.find(""succ":1");
    if (pos>=0)
        return TRUE;
    else
        return FALSE;
}
复制代码
原文地址:https://www.cnblogs.com/lidabo/p/4177020.html