#include "CRUL_server_call_httpserver.h" #include "dfl_custom.h" #include "util.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 replace(string str, string s1, string s2) { /*int pos; pos = str.find(s1); while (pos != -1) { // str.length()求字符的长度,注意str必须是string类型 str.replace(pos, string(s1).length(), s2); pos = str.find(s1); }*/ vector vec; Split(str, s1, vec); string res = ""; for (int i = 0; i < vec.size(); i++) { if (res.size() > 0) { res.append(s2); } res.append(vec[i]); } return res; } 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 gfpot01测试 string callHttpserver2(string signinfoJsonString, char* url) { 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://gfpot01: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_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); string key = "Authorization: Basic "; if (tc_strstr(url, "10.10.101.219") == NULL && tc_strstr(url, "gfpoapp") == NULL) { key.append("cG9fcmZjOjFxYXohUUFa"); } else { key.append("c2hwbG06c2FwQDIwMTk="); } struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/xml"); //cG9fcmZjOjFxYXohUUFa测试 headers = curl_slist_append(headers, key.c_str()); //c2hwbG06c2FwQDIwMTk=正式 headers = curl_slist_append(headers, "Cookie: JSESSIONID=-E6lQ1chbNdpncO4u4bcyWTq975hjwH2lQQB_SAPqDb0iHxoW4xB8Tlcn2TAho7J; JSESSIONMARKID=5HZ08QQuXWa7hQoZajm-ZJRySpCVLLwytn1_aVBAE; MYSAPSSO2=AjExMDAgAA1wb3J0YWw6cG9fcmZjiAAHZGVmYXVsdAEABlBPX1JGQwIAAzAwMAMAA1BJVAQADDIwMjQwNTEwMDkwNAUABAAAAAgKAAZQT19SRkP%2FAQQwggEABgkqhkiG9w0BBwKggfIwge8CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHATGBzzCBzAIBATAiMB0xDDAKBgNVBAMTA1BJVDENMAsGA1UECxMESjJFRQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMjQwNTEwMDkwNDE2WjAjBgkqhkiG9w0BCQQxFgQUuzRiZLjaMRv5dYl%2FRB9b2rsfWyQwCQYHKoZIzjgEAwQuMCwCFE6u4YZRTlITKJx2!2UzEsO7e1QkAhRTF5kPjf4ueRx7Qt!JrN%2FZ0iWS1A%3D%3D; saplb_*=(J2EE17077720)17077750"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //string xmlStr = replace(signinfoJsonString, "\"", "\\\""); //xmlStr = replace(xmlStr, "\r", ""); //xmlStr = replace(xmlStr, "\n", ""); const char* data = G2U(signinfoJsonString.c_str()); printf("xmlStr==> %s\n", data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out); 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; }