#include "CRUL_server_call_httpserver.h" #include #include #include #include //#define HTTP_HOST "localhost" #define HTTP_POST 9293 size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream) { string data((const char*)ptr, (size_t)size * nmemb); *((stringstream*)stream) << data << endl; return size * nmemb; } string callHttpserver(string signinfoJsonString, string url) { CURL* curl; CURLcode res; curl = curl_easy_init(); std::stringstream out; if (curl) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http"); struct curl_slist* headers = NULL; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); const char* data = signinfoJsonString.c_str(); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); res = curl_easy_perform(curl); if (res != 0) { string errMessage = curl_easy_strerror(res); } printf("返回值:%d\n", res); } string str_json = out.str(); curl_easy_cleanup(curl); printf("str_json===>%s\n", str_json.c_str()); return str_json; } string callFsHttpserver(string signinfoJsonString, string url) { CURL* curl; CURLcode res; curl = curl_easy_init(); std::stringstream out; if (curl) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); struct curl_slist* headers = NULL; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); const char* data = signinfoJsonString.c_str(); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); res = curl_easy_perform(curl); if (res != 0) { string errMessage = curl_easy_strerror(res); } printf("返回值:%d\n", res); } string str_json = out.str(); curl_easy_cleanup(curl); printf("str_json===>%s\n", str_json.c_str()); return str_json; } //gfpoapp正式 192.168.0.184测试 string callHttpserver2(string signinfoJsonString) { CURL* curl; CURLcode res; std::stringstream out; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(curl, CURLOPT_URL, "http://gfpoapp:50100/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_CHINT_ELECTRIC&receiverParty=&receiverService=&interface=SIO_BOM_PLM_ASYN&interfaceNamespace=urn%3Achintelectric.com%3Axi%3Aplm"); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http"); struct curl_slist* headers = NULL; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); headers = curl_slist_append(headers, "Content-Type: application/xml"); //c2hwb2RldjpzYXBAMjAxOQ==测试 headers = curl_slist_append(headers, "Authorization: Basic c2hwbG06c2FwQDIwMTk="); //c2hwbG06c2FwQDIwMTk=正式 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); const char* data = signinfoJsonString.c_str(); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); res = curl_easy_perform(curl); if (res != 0) { string errMessage = curl_easy_strerror(res); } printf("返回值:%d\n", res); } string str_json = out.str(); curl_easy_cleanup(curl); printf("str_json===>%s\n", str_json.c_str()); return str_json; } string callHttpGet(string url) { void* curl = curl_easy_init(); // 设置URL std::stringstream out; curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // 设置接收数据的处理函数和存放变量 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); // 执行HTTP GET操作 CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } // 接受数据存放在out中,输出之 //cout << out.str() << endl; string str_json = out.str(); curl_easy_cleanup(curl); printf("str_json===>%s\n", str_json.c_str()); return str_json; }