|
当使用C++做HTTP客户端时,目前通用的做法就是使用libcurl。其官方网站的地址是http://curl.haxx.se/,该网站主要提供了Curl和libcurl。Curl是命令行工具,用于完成FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP的命令的请求及接收回馈。libcurl提供给开发者,用于使用C++跨平台的开发各种网络协议的请求及响应。里面的文档非常齐全,不过都是英文的。
本文提供最简单的demo使用libcurl开发HttpClient。主要包括同步的HTTP GET、HTTP POST、HTTPS GET、HTTPS POST。
下载libcurl包,如果使用Linux平台,建议下载源文件编译;如果使用Windows平台,建议下载Win32 - MSVC,下载地址是:http://curl.haxx.se/download.html
- #ifndef__HTTP_CURL_H__
- #define__HTTP_CURL_H__
- #include<string>
- classCHttpClient
- {
- public:
- CHttpClient(void);
- ~CHttpClient(void);
- public:
- intPost(conststd::string&strUrl,conststd::string&strPost,std::string&strResponse);
- intGet(conststd::string&strUrl,std::string&strResponse);
- intPosts(conststd::string&strUrl,conststd::string&strPost,std::string&strResponse,constchar*pCaPath=NULL);
- intGets(conststd::string&strUrl,std::string&strResponse,constchar*pCaPath=NULL);
- public:
- voidSetDebug(boolbDebug);
- private:
- boolm_bDebug;
- };
- #endif
- #include"httpclient.h"
- #include"curl/curl.h"
- #include<string>
- CHttpClient::CHttpClient(void):
- m_bDebug(false)
- {
- }
- CHttpClient::~CHttpClient(void)
- {
- }
- staticintOnDebug(CURL*,curl_infotypeitype,char*pData,size_tsize,void*)
- {
- if(itype==CURLINFO_TEXT)
- {
- }
- elseif(itype==CURLINFO_HEADER_IN)
- {
- printf("[HEADER_IN]%s\n",pData);
- }
- elseif(itype==CURLINFO_HEADER_OUT)
- {
- printf("[HEADER_OUT]%s\n",pData);
- }
- elseif(itype==CURLINFO_DATA_IN)
- {
- printf("[DATA_IN]%s\n",pData);
- }
- elseif(itype==CURLINFO_DATA_OUT)
- {
- printf("[DATA_OUT]%s\n",pData);
- }
- return0;
- }
- staticsize_tOnWriteData(void*buffer,size_tsize,size_tnmemb,void*lpVoid)
- {
- std::string*str=dynamic_cast<std::string*>((std::string*)lpVoid);
- if(NULL==str||NULL==buffer)
- {
- return-1;
- }
- char*pData=(char*)buffer;
- str->append(pData,size*nmemb);
- returnnmemb;
- }
- intCHttpClient::Post(conststd::string&strUrl,conststd::string&strPost,std::string&strResponse)
- {
- CURLcoderes;
- CURL*curl=curl_easy_init();
- if(NULL==curl)
- {
- returnCURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
- curl_easy_setopt(curl,CURLOPT_DEBUGFUNCTION,OnDebug);
- }
- curl_easy_setopt(curl,CURLOPT_URL,strUrl.c_str());
- curl_easy_setopt(curl,CURLOPT_POST,1);
- curl_easy_setopt(curl,CURLOPT_POSTFIELDS,strPost.c_str());
- curl_easy_setopt(curl,CURLOPT_READFUNCTION,NULL);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,OnWriteData);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&strResponse);
- curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1);
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,3);
- curl_easy_setopt(curl,CURLOPT_TIMEOUT,3);
- res=curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- returnres;
- }
- intCHttpClient::Get(conststd::string&strUrl,std::string&strResponse)
- {
- CURLcoderes;
- CURL*curl=curl_easy_init();
- if(NULL==curl)
- {
- returnCURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
- curl_easy_setopt(curl,CURLOPT_DEBUGFUNCTION,OnDebug);
- }
- <prename="code"class="cpp">curl_easy_setopt(curl,CURLOPT_URL,strUrl.c_str());
- curl_easy_setopt(curl,CURLOPT_READFUNCTION,NULL);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,OnWriteData);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&strResponse);
- curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1);
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,3);
- curl_easy_setopt(curl,CURLOPT_TIMEOUT,3);
- res=curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- returnres;
- }
- intCHttpClient::Posts(conststd::string&strUrl,conststd::string&strPost,std::string&strResponse,constchar*pCaPath)
- {
- CURLcoderes;
- CURL*curl=curl_easy_init();
- if(NULL==curl)
- {
- returnCURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
- curl_easy_setopt(curl,CURLOPT_DEBUGFUNCTION,OnDebug);
- }
- curl_easy_setopt(curl,CURLOPT_URL,strUrl.c_str());
- curl_easy_setopt(curl,CURLOPT_POST,1);
- curl_easy_setopt(curl,CURLOPT_POSTFIELDS,strPost.c_str());
- curl_easy_setopt(curl,CURLOPT_READFUNCTION,NULL);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,OnWriteData);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&strResponse);
- curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1);
- if(NULL==pCaPath)
- {
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,false);
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,false);
- }
- else
- {
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,true);
- curl_easy_setopt(curl,CURLOPT_CAINFO,pCaPath);
- }
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,3);
- curl_easy_setopt(curl,CURLOPT_TIMEOUT,3);
- res=curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- returnres;
- }
- intCHttpClient::Gets(conststd::string&strUrl,std::string&strResponse,constchar*pCaPath)
- {
- CURLcoderes;
- CURL*curl=curl_easy_init();
- if(NULL==curl)
- {
- returnCURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
- curl_easy_setopt(curl,CURLOPT_DEBUGFUNCTION,OnDebug);
- }
- curl_easy_setopt(curl,CURLOPT_URL,strUrl.c_str());
- curl_easy_setopt(curl,CURLOPT_READFUNCTION,NULL);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,OnWriteData);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&strResponse);
- curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1);
- if(NULL==pCaPath)
- {
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,false);
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,false);
- }
- else
- {
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,true);
- curl_easy_setopt(curl,CURLOPT_CAINFO,pCaPath);
- }
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,3);
- curl_easy_setopt(curl,CURLOPT_TIMEOUT,3);
- res=curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- returnres;
- }
- voidCHttpClient::SetDebug(boolbDebug)
- {
- m_bDebug=bDebug;
- }
- </pre><p></p>
- <pre></pre>
- <br>
- <br>
- <p></p>
- <br>
|