C++ 使用post/get进行发送数据
2017-09-18 19:25
405 查看
使用开源的libcur实现post发送数据。libcur的使用请自行百度。下面是我的代码,用post发送过个类型的数据
注意:代码不能直接使用,因为我里面依赖了其他文件和类,这里这是做一个示例
注意:代码不能直接使用,因为我里面依赖了其他文件和类,这里这是做一个示例
//httpcurl.h #include <string> #include "SqlOperation.h" class CFaceRecognice; class CHttpClient { public: CHttpClient(CSqlOperation* sqloperate); ~CHttpClient(void); public: int Post(const std::string strUrl, int found_int, string guid_str, BYTE* feat_byte, BYTE* photo_byte, long picsize); private: bool m_bDebug; CSqlOperation* m_sqloperate; };
//httpcurl.cpp #include "stdafx.h" #include "httpcurl.h" #include "curl.h" #include "json/json.h" CHttpClient::CHttpClient(CSqlOperation* sqloperate) //CSqlOperation 是我其他类中自己定义的一个类,可以不传入 :m_bDebug(false) ,m_sqloperate(sqloperate) { } CHttpClient::~CHttpClient(void) { } string Utf82Ansi(const char* utf8) { int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL); LPWSTR pszW = (LPWSTR)_alloca((WLength + 1) *sizeof(WCHAR)); MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength); pszW[WLength] = 0; int ALength = WideCharToMultiByte(CP_ACP, 0, pszW, -1, NULL, 0, NULL, NULL); LPSTR pszA = (LPSTR)_alloca(ALength + 1); WideCharToMultiByte(CP_ACP, 0, pszW, -1, pszA, ALength, NULL, NULL); pszA[ALength] = 0; return pszA; } //post返回调用的函数,如果没定义此函数则会有一个默认的函数 //如果需要给OnWriteData传递指针,则传递给lpVoid void OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) { if (NULL == buffer) { return; } Json::Reader reader; Json::Value json_value; if (reader.parse((char*)buffer, json_value)) // reader将Json字符串解析到root,root将包含Json里所有子元素 { if (!json_value["found_c"].isNull()) { int face_found = json_value["found_c"].asInt(); if (!face_found) //客户端数据库中没有找到face信息,认为是新人 { if (!json_value["guid_c"].isNull() && !json_value["guid_s"].isNull()) { string guid_c = Utf82Ansi(json_value["guid_c"].asString().c_str()); string guid_s = Utf82Ansi(json_value["guid_s"].asString().c_str()); if (strcmp(guid_c.c_str(), guid_s.c_str()) != 0) { CSqlOperation* sql = (CSqlOperation*)lpVoid; sql->UpdateGUID(guid_c, guid_s); //此处更新数据库 } } } } } return; } char* Unicode2Utf8(const TCHAR* unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL); return (char*)szUtf8; } BOOL StringToWString0(const std::string &str, std::wstring &wstr) { int nLen = (int)str.length(); wstr.resize(nLen, L' '); int nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, (LPWSTR)wstr.c_str(), nLen); if (nResult == 0) { return FALSE; } return TRUE; } /* post发送了三种类型的数据 found:整形数据 guid:字符型数据 feat:二进制字节流数据 photo:二进制字节流数据,其实是一个图片 */ int CHttpClient::Post(const std::string strUrl, int found_int, string guid_str, BYTE* feat_byte, BYTE* photo_byte,long picsize) { CURLcode res; struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; CURL* curl = curl_easy_init(); if (NULL == curl) { return CURLE_FAILED_INIT; } if (found_int) { curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("found")), CURLFORM_COPYCONTENTS, "1", CURLFORM_END); } else { curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("found")), CURLFORM_COPYCONTENTS, "0", CURLFORM_END); } wstring wguid; StringToWString0(guid_str, wguid); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("guid")), CURLFORM_COPYCONTENTS, Unicode2Utf8(wguid.c_str()), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("feat")), CURLFORM_BUFFER, "data", CURLFORM_BUFFERPTR, feat_byte, CURLFORM_BUFFERLENGTH, sizeof(float)*2048, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("photo")), CURLFORM_BUFFER, "data", CURLFORM_BUFFERPTR, photo_byte, CURLFORM_BUFFERLENGTH, sizeof(float)* 2048, CURLFORM_END); string url = "http://127.0.0.1:8080/apiReport.do"; //这是我的url地址,根据自己的需求做更改 struct curl_slist *headers = NULL; //设置post的一些相关信息 headers = curl_slist_append(headers, "POST"); headers = curl_slist_append(headers, "Connection: Keep-Alive"); headers = curl_slist_append(headers, "Accept: */*"); headers = curl_slist_append(headers, "Content-Type: multipart/form-data;"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITE a8a2 FUNCTION, OnWriteData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, m_sqloperate); //给OnWriteData传递的指针参数 res = curl_easy_perform(curl); curl_formfree(formpost); curl_easy_cleanup(curl); return res; }
相关文章推荐
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- 使用jQuery的ajax的post与get发送数据到指定url
- 使用C++ stringstream来进行数据类型转换
- Android开发使用GET方式向服务器请求和发送数据
- 使用RESTClient插件进行数据模拟(GET,POST)提交
- C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
- C++网络编程 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- 使用安卓的http进行数据的发送需要做的首先就是授权
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- python requests 自动管理 cookie 。 get后进行post发送数据---》最简单的刷票
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- [C++]数据结构实验04:使用堆栈进行简单的四则运算
- httpClient使用,1 发送post请求 2 发送get请求取得接口中的数据