环境:Win7 64位+VC2008
软件及源码下载:(http://pan.baidu.com/s/1jGE52pK)
涉及到的知识点:
libcurl的使用(包括发送http请求、发送cookie给服务器、保存cookie)
关于libcurl的资料,推荐大家参考下官方文档:http://curl.haxx.se/libcurl/c/example.html
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
char *curl_version( );
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
读取本地的cookie.txt文件中的cookie信息
1
|
curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,
"cookie.txt"
);
|
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; }