#include"cJSON.h"
#include<iostream>
#include<string>
#include<QString>
#include "curl/curl.h"
using namespace std;
const string CAL_HOST_URL = "http://127.0.0.1:31105";
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
{
string *str = (string*)stream;
(*str).append((char*)ptr, size*nmemb);
return size * nmemb;
}
CURLcode CurlPostReq(const string &url, const string &postParams, string &response)
{
CURL *curl = curl_easy_init();
CURLcode res;
if (curl)
{
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
return res;
}
void post_dataCleansing() {
curl_global_init(CURL_GLOBAL_ALL);
string postUrlStr = CAL_HOST_URL + "/data_cleansing";
cJSON_AddItemToObject(paramRoot, "padding_value", cJSON_CreateNumber(1));
cJSON_AddItemToObject(paramRoot, "resampling_rate", cJSON_CreateNumber(2));
char *paramData = NULL;
paramData = cJSON_PrintUnformatted(paramRoot);
string postParams = paramData;
string postResponseStr;
auto res = CurlPostReq(postUrlStr, postParams, postResponseStr);
if (res != CURLE_OK)
cout << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)) << endl;
else
cout << postResponseStr << endl;
curl_global_cleanup();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)