Libcurl 以JSON模式POST

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();//这里需要转UTF8

	SMART_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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值