C 使用libcurl做HttpClient

C++使用libcurl做HttpClient

当使用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 class CHttpClient { public:

CHttpClient(void); ~CHttpClient(void); public: /** * @brief HTTP POST请求 * @param strUrl 输入参数,请求的Url地址,如:http://www.http://www.35331.cn/ *

@param strPost 输入参数,使用如下格式

para1=val1?2=val2&… * @param strResponse 输出参数,返回的内容 * @return 返回是否Post成功 */ int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse); /** * @brief HTTP GET请求 * @param strUrl 输入参数,请求的Url地址,如:http://www.http://www.35331.cn/ * @param strResponse 输出参数,返回的内容 * @return 返回是否Post成功 */ int Get(const std::string & strUrl, std::string & strResponse); /** * @brief HTTPS POST请求,无证书版本 * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com * @param strPost 输入参数,使用如下格式para1=val1?2=val2&… * @param strResponse 输出参数,返回的内容 * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. * @return 返回是否Post成功 */ int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL); /** * @brief HTTPS GET请求,无证书版本 * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com * @param strResponse 输出参数,返回的内容 *

@param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. * @return 返回是否Post成功 */ int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL); public: void SetDebug(bool bDebug); private: bool m_bDebug; }; #endif [cpp] view plaincopy#include \ #include \ #include CHttpClient::CHttpClient(void) :

m_bDebug(false) { } CHttpClient::~CHttpClient(void) { } static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *) { if(itype == CURLINFO_TEXT)

{ //printf(\ } else if(itype == CURLINFO_HEADER_IN)

{ printf(\ } else if(itype == CURLINFO_HEADER_OUT)

{ printf(\ } else if(itype == CURLINFO_DATA_IN)

{ printf(\ } else if(itype == CURLINFO_DATA_OUT)

{ printf(\ } return 0; } static size_t OnWriteData(void* buffer, size_t

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4