libcurl的HTTP客户端示例

头文件

#pragma once

#include <map>
#include <string>

#include <curl/curl.h>

/*
测试环境:vs2010SP1、Win7 32bits
使用说明:
[1]初始化libcurl资源
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
[2]释放libcurl资源
curl_easy_cleanup(curl);
curl_global_cleanup();
*/

namespace kagula
{
	CURLcode ReponseFromUrlByGetMethod(CURL * easy_handle, std::string url, std::string proxyAddress, int proxyType, std::string &outDataHeader, std::string &outData);
	//postData参数,"param1=value1¶m2=value2¶m3=value3"
	CURLcode ReponseFromUrlByPostMethod(CURL *curl_handle, std::string url,std::string postData, std::string &outDataHeader,std::string &outData);

	std::string GetCookieValue(std::string key);
	void GetAllCookie(std::map<std::string,std::string> &mapValue);
}


源文件

#include "curl736.h"
#include <list>
#include <vector>

namespace kagula
{
	/************************************************************************/
	/* http://curl.haxx.se/                                                 */
	/************************************************************************/
	const std::string g_strAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
	const std::string g_strCookie = "cookie.txt";
	const int g_timeout = 3;
	std::list<std::string> g_listCookie;


	size_t write_callback( void *ptr, size_t size, size_t nmemb, void *outBuffer )
	{
		int len = size * nmemb;
		char *temp = new char[len+1];	
		memcpy(temp,ptr,len);
		temp[len]=0;
		reinterpret_cast<std::string *>(outBuffer)->append(temp);
		delete temp;
		return len;
	}

	void print_cookies(CURL *curl)
	{
		CURLcode res;
		struct curl_slist *cookies;
		struct curl_slist *nc;
		int i;

		//printf("Cookies, curl knows:\n");

		res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
		if (res != CURLE_OK) {
			fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
			exit(1);
		}
		nc = cookies, i = 1;
		while (nc) {
			//printf("[%d]: %s\n", i, nc->data);

			//kagula.begin
			std::string record = nc->data;
			g_listCookie.push_back(record);
			//kagula.end
			nc = nc->next;
			i++;
		}
		if (i == 1) {
			//printf("(none)\n");
		}
		curl_slist_free_all(cookies);
	}

	CURLcode ReponseFromUrlByGetMethod(CURL * easy_handle, std::string url, std::string proxyAddress, int proxyType, std::string &outDataHeader, std::string &outData)
	{
		// 设置easy handle属性
		curl_easy_setopt(easy_handle, CURLOPT_USERAGENT,g_strAgent.c_str());
		curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION,TRUE);   
		curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());  
		curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT,g_timeout);  

		curl_easy_setopt(easy_handle, CURLOPT_VERBOSE,1);  //控制台项目,在控制台下打印具体信息

		curl_easy_setopt(easy_handle, CURLOPT_POST,0);   
		curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_callback);  
		curl_easy_setopt(easy_handle, CURLOPT_WRITEHEADER, &outDataHeader);  
		curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &outData);  
		curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,g_strCookie.c_str());  
		curl_easy_setopt(easy_handle, CURLOPT_COOKIEJAR, g_strCookie.c_str());  

		//libcurl7.36+openssl-0.9.8k_WIN32,For https protocol support!
		curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYHOST, 1); //no longer support!

		if (proxyAddress.empty()==false)
		{
			curl_easy_setopt(easy_handle, CURLOPT_PROXY, proxyAddress.c_str());//"proxy-host.com:8080"
			curl_easy_setopt(easy_handle, CURLOPT_PROXYTYPE, proxyType);//缺省为0,即HTTP代理。CURLPROXY_SOCKS4。
			curl_easy_setopt(easy_handle, CURLOPT_HTTPPROXYTUNNEL, 1L);
		}
		CURLcode code =  curl_easy_perform(easy_handle);   
		//print_cookies(easy_handle);  

		return code;
	}


	CURLcode ReponseFromUrlByPostMethod(CURL *curl_handle, std::string url,std::string postData, 
		std::string &outDataHeader,std::string &outData)
	{
		curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
		curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);
		curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,g_strAgent.c_str()); 
		curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT,g_timeout);

		curl_easy_setopt(curl_handle, CURLOPT_VERBOSE,1);

		curl_easy_setopt(curl_handle, CURLOPT_POST,1); 
		curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS,postData.c_str());
		curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE,postData.size());

		curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, g_strCookie.c_str()); 
		curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, g_strCookie.c_str()); 
		curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
		curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, &outDataHeader);
		curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &outData);

		CURLcode code = curl_easy_perform(curl_handle); 
		g_listCookie.clear();
		print_cookies(curl_handle);

		return 	code;
	}

	std::string GetCookieValue(std::string key)
	{
		std::list<std::string>::iterator iter = g_listCookie.begin();
		while( iter != g_listCookie.end() )
		{
			int nPos = iter->find(key);
			if (nPos>0)
			{
				std::string data = iter->substr(nPos+key.size(),iter->size() -nPos - key.size());
				std::string trimstring = "\t";
				data.erase(data.find_last_not_of(trimstring)+1);
				data.erase(0,data.find_first_not_of(trimstring));
				return data;
			}//end if
			iter++;
		}//end while

		return std::string();
	}//end func

	void splitStr(std::string& s, std::string& delim,std::vector< std::string > &ret)  
	{  
		size_t last = 0;
		size_t index=s.find_first_of(delim,last);
		while (index!=std::string::npos)
		{
			ret.push_back(s.substr(last,index-last));
			last=index+1;
			index=s.find_first_of(delim,last);

			if (index-last>0)
			{
				ret.push_back(s.substr(last,index-last));
			}
		}
	}//end func

	void GetAllCookie(std::map<std::string,std::string>  &mapValue)
	{
		std::list<std::string>::iterator iter = g_listCookie.begin();
		while( iter != g_listCookie.end() )
		{
			std::vector<std::string> vec;
			splitStr(*iter,std::string("\t"),vec);

			if (vec.size()<12)
			{
				continue;
			}//end if

			mapValue[vec[10]]=vec[11];

			iter++;
		}//end while
	}//end func
}//end namespace


你可能感兴趣的:(libcurl的HTTP客户端示例)