libcurl模拟登录

get方式登录


bool getLogin(string getFields, string url) { 

	CURL *curl_handle = NULL;
	CURLcode res = CURLE_FAILED_INIT;
	string szbuffer;
	string szheader_buffer;
	struct curl_slist *chunk = NULL;
	chunk = curl_slist_append(chunk, "Accept-Encoding: gzip, deflate");
	chunk = curl_slist_append(chunk, "Accept-Language: en-US,en;q=0.8");
	chunk = curl_slist_append(chunk, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");
	
	curl_global_init(CURL_GLOBAL_ALL);
	curl_handle = curl_easy_init();

	curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, chunk);
	curl_easy_setopt(curl_handle, CURLOPT_URL, (url + getFields).c_str());
	curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 5);   //设置重定向的最大次数
	curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);   //设置301、302跳转跟随location
	curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookie.dat");
	curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookie.dat");
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, FALSE);
	//抓取内容后,回调函数  
	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, call_wirte_func);
	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &szbuffer);
	//抓取头信息,回调函数  
	curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, header_callback);
	curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &szheader_buffer);
	res = curl_easy_perform(curl_handle);
	curl_easy_cleanup(curl_handle);
	curl_slist_free_all(chunk);
	chunk = NULL;
	curl_global_cleanup();
	if (res == CURLE_OK)
	{
	<span style="white-space:pre">	</span>return true;
	}
	return false;
}


post方式登录


postLogin(string postFields, string url) {
	CURL *curl_handle = NULL;
	CURLcode res = CURLE_FAILED_INIT;
	string szbuffer;
	string szheader_buffer;
	struct curl_slist *chunk = NULL;
	chunk = curl_slist_append(chunk, "Accept-Encoding: gzip, deflate");
	chunk = curl_slist_append(chunk, "Accept-Language: en-US,en;q=0.8");
	chunk = curl_slist_append(chunk, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");

	curl_global_init(CURL_GLOBAL_ALL);
	curl_handle = curl_easy_init();

	curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, chunk);
	curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
	curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 5);   //设置重定向的最大次数
	curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);   //设置301、302跳转跟随location
	curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookie.dat");
	curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookie.dat");
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, FALSE);
	//抓取内容后,回调函数  
	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, call_wirte_func);
	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &szbuffer);
	//抓取头信息,回调函数  
	curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, header_callback);
	curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &szheader_buffer);
	curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postFields.c_str());
	res = curl_easy_perform(curl_handle);
	curl_easy_cleanup(curl_handle);
	curl_slist_free_all(chunk);
	chunk = NULL;
	curl_global_cleanup();
	if (res == CURLE_OK)
	{
		return true;
	}
	return false;
}

回调函数

size_t call_wirte_func(const char *ptr, size_t size, size_t nmemb, std::string *stream)
{
	size_t len = size * nmemb;
	stream->append(ptr, len);
	return len;
}
    
size_t header_callback(const char  *ptr, size_t size, size_t nmemb, std::string *stream)
{
	size_t len = size * nmemb;
	stream->append(ptr, len);
	return len;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值