1,用到j(luò)soncpp 2,https base驗證 3,json串有中文因為要post httpserver 因此conver函數(shù)轉(zhuǎn)換utf-8#include <iostream> #include "curl.h" #include "json/json.h" #include <time.h> using namespace std; size_t write_data(void* buffer, size_t size, size_t nmemb, void* lpVoid) { std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid); if( NULL == str || NULL == buffer ) { return -1; } char* pData = (char*)buffer; str->append(pData, size * nmemb); printf("%s\n", pData); return nmemb; } int convert(const char *src, char *dest, int destlen) { wchar_t *pUnicode = new wchar_t[destlen]; int num; memset(pUnicode, 0, destlen); num = MultiByteToWideChar(CP_ACP, 0, (src), -1, (LPWSTR)pUnicode, destlen); if (0 == num) { delete[] pUnicode; return -1; } num = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pUnicode, -1, (char*)(dest), destlen, NULL, NULL); delete[] pUnicode; if (0 == num) { return -1; } return num; int main(int argc, char **argv) { // 初始化 curl_global_init(CURL_GLOBAL_ALL); CURLM *multi_handle = NULL; CURL *easy_handle1 = NULL; CURL *easy_handle2 = NULL; int still_running; string strResponse; multi_handle = curl_multi_init(); easy_handle1 = curl_easy_init(); easy_handle2 = curl_easy_init(); if(multi_handle && easy_handle1 && easy_handle2) { char *data = "{\"platform\":\"all\",\"audience\":\"all\",\"alias\": [\"wowh\",\"liup\",\"xuz\"],\"notification\": {\"alert\": \"09-21 14:41:50 303A店鋪發(fā)生剩余電流報警/異常,數(shù)值為1000mA\",\"android\": {\"builder_id\": 3,\"extras\": {\"alarm_time\": 1442817710,\"entity_id\": 134,\"entity_name\": \"配電柜A\",\"type\": \"entity_alarm\"},\"title\":\"配電柜報警\"},\"ios\": {\"badge\": 1,\"extras\": {\"alarm_time\": 1442817710,\"entity_id\": 134,\"entity_name\": \"配電柜A\",\"type\":\"entity_alarm\"},\"sound\":\"sound.caf\"}}}"; Json::Value pushData; Json::Reader reader; if(reader.parse(data, pushData)) { printf("%s=== 解析\n", pushData["platform"].asString().c_str()); } string out = pushData.toStyledString(); //Json::FastWriter writer; //std::string out = writer.write(root); printf("%s\n", out.c_str()); char dest[1024] = {0}; convert(out.c_str(), dest, 1024); curl_easy_setopt(easy_handle1, CURLOPT_URL, "https://api./v3/push"); curl_easy_setopt(easy_handle1, CURLOPT_POST, 1); curl_easy_setopt(easy_handle1, CURLOPT_POSTFIELDS, dest); curl_easy_setopt(easy_handle1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_easy_setopt(easy_handle1, CURLOPT_USERPWD, "87ec4b9217264257cfcb4e2e:1166e1faea733014d0890aeb"); curl_easy_setopt(easy_handle1, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(easy_handle1, CURLOPT_SSL_VERIFYHOST, false); curl_easy_setopt(easy_handle1, CURLOPT_TIMEOUT, 10); curl_easy_setopt(easy_handle1, CURLOPT_CONNECTTIMEOUT, 3); curl_easy_setopt(easy_handle1, CURLOPT_NOSIGNAL, 1); //多線程忽略其它信號 curl_easy_setopt(easy_handle1, CURLOPT_FORBID_REUSE, 1);// 設(shè)置tcp 鏈接完成后重用 no WAIT_CLOSE curl_easy_setopt(easy_handle1, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(easy_handle1, CURLOPT_WRITEDATA, (void *)&strResponse); curl_easy_setopt(easy_handle2, CURLOPT_URL, "https:/push.cn/push"); curl_easy_setopt(easy_handle2, CURLOPT_POST, 1); curl_easy_setopt(easy_handle2, CURLOPT_POSTFIELDS, dest); curl_easy_setopt(easy_handle2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_easy_setopt(easy_handle2, CURLOPT_USERPWD, "87ec4b92fcb4e2e:1164d0890aeb"); curl_easy_setopt(easy_handle2, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(easy_handle2, CURLOPT_SSL_VERIFYHOST, false); curl_easy_setopt(easy_handle2, CURLOPT_TIMEOUT, 10); curl_easy_setopt(easy_handle2, CURLOPT_CONNECTTIMEOUT, 3); curl_easy_setopt(easy_handle2, CURLOPT_NOSIGNAL, 1); //多線程忽略其它信號 curl_easy_setopt(easy_handle2, CURLOPT_FORBID_REUSE, 1);// 設(shè)置tcp 鏈接完成后重用 no WAIT_CLOSE curl_easy_setopt(easy_handle2, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(easy_handle2, CURLOPT_WRITEDATA, (void *)&strResponse); curl_multi_add_handle(multi_handle, easy_handle1); curl_multi_add_handle(multi_handle, easy_handle2); curl_multi_perform(multi_handle, &still_running); do { struct timeval timeout; int rc; /* select() return code */ CURLMcode mc; /* curl_multi_fdset() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); if(mc != CURLM_OK) { fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); break; } if(maxfd == -1) { #ifdef _WIN32 Sleep(100); rc = 0; #else /* Portable sleep for platforms other than Windows. */ struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ rc = select(0, NULL, NULL, NULL, &wait); #endif } else { rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); } switch(rc) { case -1: /* select error */ break; case 0: default: /* timeout or readable/writable sockets */ printf("perform!\n"); curl_multi_perform(multi_handle, &still_running); printf("running: %d!\n", still_running); break; } } while(still_running); curl_multi_cleanup(multi_handle); curl_easy_cleanup(easy_handle1); curl_easy_cleanup(easy_handle2); } // 釋放資源 //fclose(fp_sina); //fclose(fp_sohu); curl_global_cleanup(); //printf("%s\n", strResponse.c_str()); return 0; } |
|