环境: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信息

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[];
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 >= ){
returnInfo.bReturn = TRUE;
int nStartPos = content.find("data\":");
int nEndPos = content.rfind("\"}}");
returnInfo.data = content.substr(nStartPos+,nEndPos - nStartPos-);
}else{
int nStartPos = returnVal.find("error\":");
int nEndPos = returnVal.find("data\":",nStartPos);
returnInfo.strErrorInfo = returnVal.substr(nStartPos+,nEndPos-nStartPos-);
}
//释放资源
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,); //设置请求超时时间
//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 == -)
return -;
nPos -= ;
int nStartPos = html.rfind("/",nPos);
string strTotal = html.substr(nStartPos+,nPos - nStartPos - );
return atoi(strTotal.c_str());
}

获取待评论的资源列表

static vector<DownResourceInfo> GetToCommentList(int pageNum)
{
vector<DownResourceInfo> vtDownload;
char url[] = {};
sprintf(url,"http://download.csdn.net/my/downloads/%d",pageNum);
string html = GetHtmlPage(url);
int nPos = ;
int n = ;
int flag = ;
while((nPos = html.find("#comment",n)) != -)
{
n = nPos+;
int nStartPos = html.rfind("/",nPos);
string strUrl = html.substr(nStartPos+,nPos - nStartPos -);
DownResourceInfo info;
info.strResourceCurl = strUrl;
//获取资源的名字
nStartPos = html.find(strUrl,nPos+);
if(nStartPos == -)
return vtDownload;
nStartPos += ;
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>=)
return TRUE;
else
return FALSE;
}

原文地址:曾是土木人

新浪微博:http://weibo.com/cstmr

VC使用libcurl模拟登录CSDN并自动评论资源以获取积分的更多相关文章

  1. libcurl模拟登录CSDN并自动评论资源以获取积分

    软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求.发送cookie给服务器.保存coo ...

  2. python登录csdn并自动评论下载资源脚本

    功能 1.自动登录csdn 2.查找未评论的资源并自动评论 用到的库 1.python自带的requests,获取以及发送网页数据 2.python自带的time,用作休眠,csdn资源一段时间内只允 ...

  3. 3 使用selenium模拟登录csdn

    之前通过F12开发者模式调试,获取网站后台服务器验证用户名和密码的URL之后,再构造post数据的方式会存在一个问题,就是对目标网站的验证机制不明确,构造post数据除了用户名和密码之外,还可能有更复 ...

  4. 基于python的request库,模拟登录csdn博客

    以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库     ...

  5. 4 使用Selenium模拟登录csdn,取出cookie信息,再用requests.session访问个人中心(保持登录状态)

    代码: # -*- coding: utf-8 -*- """ Created on Fri Jul 13 16:13:52 2018 @author: a " ...

  6. 2 模拟登录_Post表单方式(针对chinaunix有效,针对csdn失效,并说明原因)

    参考精通Python网络爬虫实战 首先,针对chinaunix import urllib.request #原书作者提供的测试url url="http://bbs.chinaunix.n ...

  7. htmlunit 模拟登录 无验证码

    1.模拟登录csdn,最开始的时候使用的是httpclient,网上的所有模拟登录csdn的版本都是找到lt/execution/event_id.连同用户名及密码 一起发送即可,但是目前的csdn的 ...

  8. 模拟登录神器之PHP基于cURL实现自动模拟登录类

    一.构思 从Firefox浏览器拷贝cURL命令(初始页.提交.提交后) 自动分析curl形成模拟登录代码 默认参数:ssl/302/gzip 二.实现 接口 (一)根据curl信息执行并解析结果 p ...

  9. scrapy模拟登录微博

    http://blog.csdn.net/pipisorry/article/details/47008981 这篇文章是介绍使用scrapy模拟登录微博,并爬取微博相关内容.关于登录流程为嘛如此设置 ...

随机推荐

  1. 用Word收集网页中的内容,用文档结构图整理

    如何用Word保存网页中的内容 网页中的内容,用什么保存好? 用笔记类软件是个不错的选择,还可以用 Word 保存,这样方便用“文档结构图”来整理网页. 如图:网页收集后用文档结构图进行整理. (图一 ...

  2. SQL 游标使用实例

    IF EXISTS(SELECT *FROM sysobjects WHERE name='sp_ContestSubmit') DROP PROC sp_ContestSubmit GO -- == ...

  3. pgpgin|pgpgout|pswpin|pswpout意义与差异

    引用来自: http://ssms.cs2c.com.cn/otrs/pc.pl?Action=PublicFAQZoom;ItemID=11741 文章主要意思是: 1. page in/out操作 ...

  4. JS错误 theForm.submit();SCRIPT3: 找不到成员。

    最近发现一个问题 当我点击 gridview中 “修改” 按钮时 会出现如下错误. 通过一系列的排查,也没有发现什么错误..后来发现在页面中 有一个按钮 的id为submit . 于是更改这个 but ...

  5. 如何让Targetprocess 中 webhook 推送comment 到指定的项目

    Targetprocess 作为Agile管理工具非常好使.我们用TP + bearychat来做任务的沟通. TP目前是不支持comment push到指定的项目,因为其带出的字段中没有相关项目.但 ...

  6. CoreImage 处理图片

    1.CoreImage 滤镜的使用(马赛克模糊) CoreImage是苹果公司为了简化图片处理的难度而开发出来的类库. 随着iOS版本号升级以及硬件性能的不断提升,CoreImage将支持越来越多的滤 ...

  7. java生成解析xml的另外两种方法Xstream

    Xstream生成和解析xm和JAXB生成和解析xml的方法. 一,Xstream Xstream非jdk自带的,需要到入Xstream-1.4.3.jar和xpp3_min-1.1.4.jar 1. ...

  8. [Compose] 20. Principled type conversions with Natural Transformations

    We learn what a natural transformation is and see the laws it must obey. We will see how a natural t ...

  9. 1. 初探MongoDB —— 介绍、安装和配置

    一.写在前面 之前一直以来,都是使用关系型数据库.也很早就听闻,当数据量大的时候,关系型数据库使用起来会有很多问题.诸如查询数据慢,加载花费时间长等等. 也早就耳闻NOSQL(NoSQL = Not ...

  10. [PaPaPa][需求说明书][V0.1]

    PaPaPa软件需求说明书V0.1 前   言 我觉得我们废话不能多,废话一多的话大家就找不到重点了,其实本项目是出于技术研究的目的来开发的,大家讨论了半天决定要做社(yue)交(pao)类的项目. ...