2014-03-19工作日志:第一个工作小成功:c++传值给网页
来源:互联网 发布:网络打印机文档被挂起 编辑:程序博客网 时间:2024/06/05 17:57
client.cpp#include "httpclient.h"#include "curl/curl.h"#include <string>#include <cstdio>#include <iostream>using namespace std;#pragma comment(lib,"libcurl.lib")//#pragma comment(lib,"curllib_static.lib")//#pragma comment(lib,"ssleay32.lib")//#pragma comment(lib,"libcurl_imp.lib")//#pragma comment(lib,"libeay32.lib")//#pragma comment(lib,"openldap.lib")CHttpClient::CHttpClient(void) : m_bDebug(false){}CHttpClient::~CHttpClient(void){}static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *){if(itype == CURLINFO_TEXT){//printf("[TEXT]%s\n", pData);}else if(itype == CURLINFO_HEADER_IN){printf("[HEADER_IN]%s\n", pData);}else if(itype == CURLINFO_HEADER_OUT){printf("[HEADER_OUT]%s\n", pData);}else if(itype == CURLINFO_DATA_IN){printf("[DATA_IN]%s\n", pData);}else if(itype == CURLINFO_DATA_OUT){printf("[DATA_OUT]%s\n", pData);}return 0;}static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid){std::string* str = ((std::string *)lpVoid);//dynamic_cast<std::string*>if( NULL == str || NULL == buffer ){return -1;} char* pData = (char*)buffer; str->append(pData, size * nmemb);return nmemb;}int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse){CURLcode res;CURL* curl = curl_easy_init();if(NULL == curl){return CURLE_FAILED_INIT;}if(m_bDebug){curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);}curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());curl_easy_setopt(curl, CURLOPT_POST, 1);curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);return res;}void CHttpClient::SetDebug(bool bDebug) { m_bDebug = bDebug; } int main(){CHttpClient client;string url="localhost/recieve.php";//http://192.168.0.116/lanyun/?option=ajax2&a=plan_result&type=1&nologin=1string post="type=3&mac=10:00:00:00:00:00&name=111";string psend;client.SetDebug(FALSE);int a=client.Post(url,post,psend);printf("%d\n%s\n",a,psend.c_str());return 0;}
httpclient.h#ifndef __HTTP_CURL_H__#define __HTTP_CURL_H__#include <string>class CHttpClient{public:CHttpClient(void);~CHttpClient(void);public:/*** @brief HTTP POST请求* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com* @param strPost 输入参数,使用如下格式para1=val1?2=val2&…* @param strResponse 输出参数,返回的内容* @return 返回是否Post成功*/int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);void SetDebug(bool bDebug);private:bool m_bDebug;};#endif
0 0
- 2014-03-19工作日志:第一个工作小成功:c++传值给网页
- 工作第一个月
- 给新手程序员的16个工作必备小妙招
- 给新手程序员的16个工作必备小妙招
- 给新手程序员的16个工作必备小妙招
- 第一个工作,第一个抉择
- 工作日志2014-08-19
- 2014-03-21工作日志:
- 工作日志2014-07-03
- 第一个月的工作
- 纪念工作第一个月
- 工作第一个月小结
- 第一个工作项目小结
- 外企工作日志19
- [2013.7.19] 工作日志
- 2016.08.19工作日志
- 注册成功后给个小提示
- 给新手程序员的16个工作必备小妙招,省下时间去LOL吧!
- 三角形2
- java异常处理机制
- axis2 发布webservice
- C++基础:C++中vector使用简介
- Https网站中的安全证书导入到java中的cacerts证书库
- 2014-03-19工作日志:第一个工作小成功:c++传值给网页
- requires that an attribute name is preceded by whitespace异常总结
- String,StringBuffer与StringBuilder的区别??
- 基础题1
- 新手的第一块基于FPGA的设计-脉冲信号发生器---2
- ubuntu下Pycharm安装及配置
- 奇怪的比赛--蓝桥杯
- go语言的匿名函数
- 如何使用SSH客户端putty远程连接linux