Sin descripción

HttpClient.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "stdafx.h"
  2. #include "HttpClient.h"
  3. #include <map>
  4. #include <mutex>
  5. size_t CHttpClient::req_reply(void * ptr, size_t size, size_t nmemb, void * stream)
  6. {
  7. std::string *str = (std::string*)stream;
  8. (*str).append((char*)ptr, size*nmemb);
  9. return size * nmemb;
  10. }
  11. static std::mutex mut;
  12. static std::map<std::string, int64_t> maps;
  13. BOOL CHttpClient::sendWarnMsg(const char * path)
  14. {
  15. std::unique_lock<std::mutex>lock(mut);
  16. auto it = maps.find(path);
  17. if ( it== maps.end()) // 找不到说明第一次进入
  18. {
  19. maps[path] = time(NULL);
  20. }
  21. else
  22. {
  23. int64_t timesamp = time(NULL) - it->second;
  24. maps[path] = time(NULL);
  25. if (timesamp < (5 * 60)) // 5分钟之内不提示
  26. {
  27. return false;
  28. }
  29. }
  30. lock.unlock();
  31. FILE * file;
  32. file = fopen(path, "r");
  33. if (file)
  34. {
  35. CStdioFile cfile(file);
  36. CString str;
  37. if (cfile.ReadString(str))
  38. {
  39. std::string res;
  40. const char* url = "https://oapi.dingtalk.com/robot/send?access_token=d4efe5b71f2e92a11ff89bb8ad704319e2d874ef5e590e8a78247405542bf637";
  41. Json::Value root;
  42. Json::Value child;
  43. Json::Value third;
  44. Json::StreamWriterBuilder builder;
  45. const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
  46. third["isAtAll"] = true;
  47. child["at"] = third;
  48. third.clear();
  49. child["msgtype"] = "text";
  50. third["content"] = str.GetBuffer(0);
  51. child["text"] = third;
  52. root = child;
  53. str = Json::writeString(builder, root).c_str();
  54. return curl_post_req(url, str, res) == 0 ? true : false;
  55. }
  56. cfile.Close();
  57. }
  58. return false;
  59. }
  60. CURLcode CHttpClient::curl_post_req(const char* url, const char* postParams, std::string & response)
  61. {
  62. // global init
  63. curl_global_init(CURL_GLOBAL_ALL);
  64. // init curl
  65. CURL *curl = curl_easy_init();
  66. // res code
  67. CURLcode res;
  68. if (curl)
  69. {
  70. // set params
  71. curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
  72. curl_easy_setopt(curl, CURLOPT_URL, url); // url
  73. curl_slist *headers = nullptr;
  74. headers = curl_slist_append(headers, "Content-Type:application/json");
  75. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  76. if(postParams!=nullptr)
  77. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams); // params
  78. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
  79. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true); // set peer and host verify false
  80. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  81. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  82. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
  83. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
  84. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  85. curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  86. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  87. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  88. // start req
  89. res = curl_easy_perform(curl);
  90. fprintf(stderr, "curl_easy_perform[%d] error./n", res);
  91. curl_easy_strerror(res);
  92. }
  93. // release curl
  94. curl_easy_cleanup(curl);
  95. // global release
  96. curl_global_cleanup();
  97. return res;
  98. }
  99. CHttpClient CHttpClient::sp;