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
阅读全文
0 0
- Libcurl 表单提交模式(POST 模式)
- Libcurl 以JSON模式POST
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- 使用libcurl提交POST请求
- libcurl提交表单上传文件
- libcurl提交表单上传文件
- 在Tapestry中使用redirect-after-post模式控制表单提交
- Web开发设计模式PRG:Post/Redirect/Get,防止重复提交表单
- 静态页面提交post->servlet处理模式
- js post 提交表单
- Servlet:Post提交表单
- struts2 表单提交post
- post提交表单
- 模拟post表单提交
- 使用libcurl提交POST请求(二)
- 关于jQuery中的attr和prop
- C/C++
- 网络通讯TCP和UDP的编程流程
- AGC 011
- 99乘法表
- Libcurl 表单提交模式(POST 模式)
- 简单排序算法:冒泡排序(交换排序)
- 计算机视觉识别简史:从 AlexNet、ResNet 到 Mask RCNN
- Cordova插件(语音识别--科大讯飞)
- 商业web 漏洞扫描神器———AWVS篇基础
- mysql启动命令
- 使用socket封装库-waisock编程入门
- AS connection is not connect
- ImageView铺满整个组件