Libcurl 以JSON模式POST
来源:互联网 发布:产业结构优化理论 编辑:程序博客网 时间:2024/05/29 12:42
struct SMART_DATA_CACHE{char* buf;DWORD dwTotalLen;SMART_DATA_CACHE(){len = 5 * 1024 * 1024;dwTotalLen = 0;buf = NULL;while (!buf){try{buf = new char[len];}catch (const bad_alloc& e){}}memset(buf, 0x00, len);}~SMART_DATA_CACHE(){if (buf){delete[] buf;buf = NULL;len = 0;dwTotalLen = 0;}}private:int len;};class CCustomHttp{public:CCustomHttp(void);~CCustomHttp(void);int HttpUpload(const char* httpUrl, const char* httpData, SMART_DATA_CACHE& http_recv_buf);protected:static size_t http_recv_cb(void* ptr, size_t size, size_t nmemb, void* stream);};//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//CCustomHttp *pCustomHttp = NULL;CCustomHttp::CCustomHttp(void){pCustomHttp = this;}CCustomHttp::~CCustomHttp(void){}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//size_t CCustomHttp::http_recv_cb(void* ptr, size_t size, size_t nmemb, void* stream){SMART_DATA_CACHE* pDataBuf = (SMART_DATA_CACHE*)stream;memcpy(pDataBuf->buf + pDataBuf->dwTotalLen, ptr, size*nmemb);pDataBuf->dwTotalLen += size*nmemb;return size*nmemb;}int CCustomHttp::HttpUpload(const char* httpUrl, const char* httpData, SMART_DATA_CACHE& http_recv_buf){int nErrCode = 0;if (__curl_easy_init){CURL* curl = __curl_easy_init();if (curl){struct curl_slist* http_header = NULL;http_header = __curl_slist_append(http_header, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3");http_header = __curl_slist_append(http_header, "Charset: UTF-8");http_header = __curl_slist_append(http_header, "Connection: keep-alive");//保持长连接http_header = __curl_slist_append(http_header, "Content-Type:application/json;charset=UTF-8");__curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_header);//修改协议头__curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_recv_cb);//设置接收回调__curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&http_recv_buf);//设置设置参数__curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);//设置连接时的超时时间为5秒__curl_easy_setopt(curl, CURLOPT_URL, httpUrl);//指定URL__curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);__curl_easy_setopt(curl, CURLOPT_POSTFIELDS, httpData);CURLcode res = __curl_easy_perform(curl);//执行ret = (CURLE_OK == res) ? TRUE : FALSE;__curl_slist_free_all(http_header);__curl_easy_cleanup(curl);}if (!http_recv_buf.buf){return nErrCode;}if (strstr(http_recv_buf.buf, "html")){return nErrCode;}Json::Reader reader;Json::Value root;if (reader.parse(http_recv_buf.buf, root)){// reader将Json字符串解析到root,root将包含Json里所有子元素nErrCode = root["ErrCode"].asInt();}}return nErrCode;}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//int httpReportstate(const char* cpUrl, const char* cpIP, const char* port){//Json模式Json::Value item;item["ip"] = Json::Value(cpIP);item["port"] = Json::Value(port);std::string jsonOut = item.toStyledString();//这里需要转UTF8SMART_DATA_CACHE http_recv_buf;return pCustomHttp->HttpUpload(cpUrl, jsonOut.c_str(), http_recv_buf);}
libcurl 以JSON模式post。VS2013.代码齐全。里面有JSON库已经libcurl的库:http://download.csdn.net/download/sz76211822/10107870
阅读全文
0 0
- Libcurl 以JSON模式POST
- Libcurl 表单提交模式(POST 模式)
- 使用libcurl以Post方式向HTTP服务器提交数据
- 使用libcurl以Post方式向HTTP服务器提交数据
- C语言libcurl POST json串错误“Unsupported Media Type”
- libcurl HTTP POST请求向服务器发送json数据
- httpclient4 post 以json方式传参数
- C++用libcurl通过HTTP以表单的方式Post数据到服务器
- libcurl post数据
- libcurl get post http
- libcurl post上传文件
- libcurl post 请求
- libcurl基本知识、post、get请求 -- libcurl
- 以Post方式提交并解析json数据
- volley post客户端以json数据上传服务器
- php7游戏端以post发送json数据给php
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- ES6-函数的扩展-rest参数
- 6-20 No Less Than X in BST(20 point(s))
- 什么是hashmap
- 处理Whitelabel Error Page
- at91sam9260的外部硬件时钟
- Libcurl 以JSON模式POST
- (转载)typedef使用
- springboot 集成 jpa 详解
- NOIP模拟(11.07)T2 路径统计
- 批处理命令一日一教学 (13)字符串查找增强:findstr
- 在R中来连接mysql
- 张家口模型制作java拼图
- SFP-10G-SR价格及兼容性介绍
- ios 对于修改navigationItem的颜色和系统自带的navigationItem的rightBarButtonItem的颜色的问题