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
原创粉丝点击