#include "curl/curl.h"
#include "json.h"
void test_curl(const char *url,const char * req)
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
std::string post_Response_str = "";
CURLcode res = CURL_LAST;
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_string.c_str());//传输头部
//curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers); // set params
curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
curl_easy_setopt(curl, CURLOPT_URL, url); // url传输的链接
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(req));
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&post_Response_str);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_HEADER, 1); //0
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
}
else
{
printf(“error\n”);
}
if (res != CURLE_OK)
{
std::cerr << "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)) << std::endl;
}
else
{
printf("****device_registe back:****:%s\n", post_Response_str.c_str());
}
curl_global_cleanup();
}
libcurl的post使用
最新推荐文章于 2023-07-18 15:44:00 发布