Libcurl 表单提交模式(POST 模式)

来源:互联网 发布:抽奖算法 奖池 c 编辑:程序博客网 时间:2024/06/04 05:21
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(struct curl_httppost *post, const char* http_url, 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(struct curl_httppost *post, const char* http_url, 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");//保持长连接__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, http_url);//指定URL__curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);//post方式CURLcode res = __curl_easy_perform(curl);//执行__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["nErrCode"].asInt();}}return nErrCode;}//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//int httpReportstate(const char* cpUrl, const char* cpIP, const char* cpPort){//表单模式struct curl_httppost *post = NULL;struct curl_httppost *last = NULL;__curl_formadd(&post, &last, CURLFORM_COPYNAME, "ip", CURLFORM_COPYCONTENTS, cpIP, CURLFORM_END);__curl_formadd(&post, &last, CURLFORM_COPYNAME, "port", CURLFORM_COPYCONTENTS, cpPort, CURLFORM_END);SMART_DATA_CACHE http_recv_buf;return pCustomHttp->HttpUpload(post, cpUrl, http_recv_buf);}
libcurl 以表单模式post。VS2013.代码齐全。里面有JSON库已经libcurl的库:http://download.csdn.net/download/sz76211822/10107846
原创粉丝点击