| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include "stdafx.h"
- #include "HttpClient.h"
- #include <map>
- #include <mutex>
- size_t CHttpClient::req_reply(void * ptr, size_t size, size_t nmemb, void * stream)
- {
- std::string *str = (std::string*)stream;
- (*str).append((char*)ptr, size*nmemb);
- return size * nmemb;
- }
- static std::mutex mut;
- static std::map<std::string, int64_t> maps;
- BOOL CHttpClient::sendWarnMsg(const char * path)
- {
- std::unique_lock<std::mutex>lock(mut);
- auto it = maps.find(path);
- if ( it== maps.end()) // 找不到说明第一次进入
- {
- maps[path] = time(NULL);
- }
- else
- {
- int64_t timesamp = time(NULL) - it->second;
- maps[path] = time(NULL);
- if (timesamp < (5 * 60)) // 5分钟之内不提示
- {
- return false;
- }
- }
- lock.unlock();
- FILE * file;
- file = fopen(path, "r");
- if (file)
- {
- CStdioFile cfile(file);
- CString str;
- if (cfile.ReadString(str))
- {
- std::string res;
- const char* url = "https://oapi.dingtalk.com/robot/send?access_token=d4efe5b71f2e92a11ff89bb8ad704319e2d874ef5e590e8a78247405542bf637";
-
- Json::Value root;
- Json::Value child;
- Json::Value third;
- Json::StreamWriterBuilder builder;
- const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
- third["isAtAll"] = true;
- child["at"] = third;
- third.clear();
- child["msgtype"] = "text";
- third["content"] = str.GetBuffer(0);
- child["text"] = third;
- root = child;
- str = Json::writeString(builder, root).c_str();
-
- return curl_post_req(url, str, res) == 0 ? true : false;
- }
- cfile.Close();
- }
- return false;
- }
- CURLcode CHttpClient::curl_post_req(const char* url, const char* postParams, std::string & response)
- {
- // global init
- curl_global_init(CURL_GLOBAL_ALL);
-
- // init curl
- CURL *curl = curl_easy_init();
- // res code
- CURLcode res;
- if (curl)
- {
- // set params
- curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
- curl_easy_setopt(curl, CURLOPT_URL, url); // url
- curl_slist *headers = nullptr;
- headers = curl_slist_append(headers, "Content-Type:application/json");
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- if(postParams!=nullptr)
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams); // params
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true); // set peer and host verify 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 *)&response);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_HEADER, 0);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
- // start req
- res = curl_easy_perform(curl);
- fprintf(stderr, "curl_easy_perform[%d] error./n", res);
- curl_easy_strerror(res);
- }
- // release curl
- curl_easy_cleanup(curl);
-
- // global release
- curl_global_cleanup();
- return res;
- }
- CHttpClient CHttpClient::sp;
|