使用CURL封装HttpClient
来源:互联网 发布:淘宝卖点卡 编辑:程序博客网 时间:2024/05/16 16:17
#include <curl/curl.h>#include <string>using std::string;class HttpClient{public: static size_t WriteData(char *data, size_t block_size, size_t block_count, string *response) { if(data == NULL) return 0; size_t len = block_size * block_count; response->append(data, len); return len; } static size_t WriteFile(char *data, size_t block_size, size_t block_count, FILE *file) { if(data == NULL) return 0; size_t len = block_size * block_count; fwrite(data, len, 1, file); return len; } CHttpClient():m_pcurl(NULL) { m_pcurl = curl_easy_init(); } ~CHttpClient() { curl_easy_cleanup(m_pcurl); } bool Get(const string& url, string &response, int timeout) { return Request(url, "", "get", response, timeout); } bool Put(const string &url, const string &request, string &response, int timeout) { return Request(url, request, "put", response, timeout); } bool Post(const string& url, const string &request, string &response, int timeout) { return Request(url, request, "post", response, timeout); } bool Delete(const string &url, string &response, int timeout) { return Request(url, "", "delete", response, timeout); } bool Save( const std::string& url, const char *filename, int timeout) { m_response_code = 0; FILE* file = fopen(filename, "wb"); curl_easy_reset(m_pcurl); curl_easy_setopt(m_pcurl, CURLOPT_URL, url.c_str()); curl_easy_setopt(m_pcurl, CURLOPT_WRITEFUNCTION, WriteFile); curl_easy_setopt(m_pcurl, CURLOPT_WRITEDATA, file); curl_easy_setopt(m_pcurl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(m_pcurl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(m_pcurl, CURLOPT_TIMEOUT, timeout); CURLcode rc = curl_easy_perform(m_pcurl); fclose(file); if(rc != CURLE_OK) { m_error = string(curl_easy_strerror(rc)); return false; } rc = curl_easy_getinfo(m_pcurl, CURLINFO_RESPONSE_CODE , &m_response_code); if(rc != CURLE_OK){m_error = string(curl_easy_strerror(rc));return false;} return true; }private: bool Request(const string &url, const string &request, string method, string &response, int timeout) { m_response_code = 0; response.clear();response.reserve(64 * 1024); curl_easy_reset(m_pcurl);curl_easy_setopt(m_pcurl, CURLOPT_URL, url.c_str()); curl_easy_setopt(m_pcurl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(m_pcurl, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(m_pcurl, CURLOPT_WRITEFUNCTION, WriteData); curl_easy_setopt(m_pcurl, CURLOPT_WRITEDATA, &response); if(method == "get") { curl_easy_setopt(m_pcurl, CURLOPT_HTTPGET, 1); } else if(method == "post") { curl_easy_setopt(m_pcurl, CURLOPT_POST, 1L); curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDSIZE, request.length()); curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDS, request.c_str()); } else if(method == "put") { curl_easy_setopt(m_pcurl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDSIZE, request.length()); curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDS, request.c_str()); } else if(method == "delete") { curl_easy_setopt(m_pcurl, CURLOPT_CUSTOMREQUEST, "DELETE"); } else { return false; }CURLcode rc = curl_easy_perform(m_pcurl);if(rc != CURLE_OK){m_error = string(curl_easy_strerror(rc));return false;} rc = curl_easy_getinfo(m_pcurl, CURLINFO_RESPONSE_CODE , &m_response_code); if(rc != CURLE_OK){m_error = string(curl_easy_strerror(rc));return false;}return true; } private: CURL *m_pcurl; string m_error; long m_response_code;};
以上是使用curl封装的HttpClient;工作中使用过的,默认支持长连接。
遇到过的curl缺陷与陷阱
1、对url中的空格是不进行编码的
解决方法:使用时需要对空格进行替换为:”%20”.
2、跟服务器交互时,如果跟服务失去连接会导致僵尸(阻塞在poll)
解决方式:可以设置一个超时时间
0 0
- 使用CURL封装HttpClient
- HttpClient的使用和封装
- HttpClient简单封装及使用
- curl的使用实例 cpp封装
- curl的使用实例 cpp封装
- Cocos2d-x3.1 HttpClient封装与使用
- HttpClient封装
- java curl开发记录(二) linux系统下jni封装curl,android java使用curl
- curl跨平台HttpClient
- Android中HttpURLConnection与HttpClient的使用与封装
- libghttp安装及使用封装案例(httpClient)
- java使用httpclient封装post请求和get的请求
- C++ curl跨平台HttpClient
- php - 封装 - curl类
- curl的封装
- CURL封装方法
- 对curl的封装
- Curl 简单的封装
- TreeSet有趣问题之add方法原理
- 第四章 通过示例探索 F# 和 .NET 库
- Android开发资源推荐第2季
- Hadoop-2.4.0安装和wordcount运行验证
- linux下服务器命令小结【持续更新】
- 使用CURL封装HttpClient
- oracle 执行计划
- java8大排序
- 【学习python】eclipse导入Flask报错,但程序执行正常
- 在局域网的情况下,使用真机(手机)测试web程序
- 【codeforces】 472C Design Tutorial: Make It Nondeterministic
- 在字符串中寻找连续最长的数字串
- ios 学习之 NSPredicate 模糊、精确、查询
- 在安卓和linux设备上添加event设备