CNetwork.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "pch.h"
  2. #include "CNetwork.h"
  3. #include <nlohmann/json.hpp>
  4. CNetwork::CNetwork() : m_http_ret(http_no), m_http_status(0)
  5. {
  6. Init();
  7. }
  8. CNetwork::~CNetwork(void)
  9. {
  10. UnInit();
  11. }
  12. //void CNetwork::GetServerlist()
  13. //{
  14. // keymap.clear();
  15. // std::string path = "v1/nodeproess";
  16. // m_http_ret = http_start;
  17. // auto data = GetUrl(path, keymap);
  18. // if (data.empty())
  19. // {
  20. // m_http_ret = http_f;
  21. // m_error_msg = "";
  22. // m_error_msg.append("请求错误:网络问题,错误码:").append(std::to_string(m_http_status));
  23. // return;
  24. // }
  25. // //解析数据
  26. // /*if (!m_server_list)
  27. // {
  28. // m_error_msg = "server_list为空";
  29. // return;
  30. // }
  31. //
  32. //
  33. // if (!m_server_list->parse(data))
  34. // {
  35. // m_error_msg = m_server_list->GetLastErrorA();
  36. // m_http_ret = http_f;
  37. // return;
  38. // }*/
  39. //
  40. //
  41. // m_http_ret = http_end;
  42. //}
  43. void CNetwork::Init() {
  44. }
  45. void CNetwork::UnInit()
  46. {
  47. /*if (m_server_list)
  48. {
  49. delete m_server_list;
  50. m_server_list = NULL;
  51. }
  52. if (m_userinfo)
  53. {
  54. delete m_userinfo;
  55. m_userinfo = NULL;
  56. }*/
  57. }
  58. HTTPRET CNetwork::GetHttpConnectstatus()
  59. {
  60. return m_http_ret;
  61. }
  62. LPCSTR CNetwork::GetLastErrorA()
  63. {
  64. return m_error_msg.c_str();
  65. }
  66. std::string CNetwork::GetUrl(std::string path,std::unordered_map<std::string, std::string> parame)
  67. {
  68. cpr::Parameters p;
  69. for (auto it = parame.begin(); it != parame.end(); ++it) {
  70. //auto s = fmt::format("{0}", it->first);
  71. /*p.AddParameter({ it->first, it->second }, {});*/
  72. }
  73. for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
  74. auto s = fmt::format("{0}/{1}", *it, path.c_str());
  75. cpr::Response r;
  76. if (parame.empty())
  77. {
  78. r = cpr::Get(cpr::Url{ s.c_str() });
  79. }
  80. else {
  81. r = cpr::Get(cpr::Url{ s.c_str() }, p,cpr::Header{ {"accept", "application/json"} });
  82. }
  83. if (r.status_code == 200 || r.status_code == 201)
  84. {
  85. return r.text;
  86. }
  87. else {
  88. m_http_status = r.status_code;
  89. return "";
  90. }
  91. }
  92. return std::string("");
  93. }
  94. std::string CNetwork::PostUrl(std::string path,std::unordered_map<std::string, std::string> parame, std::string data)
  95. {
  96. std::vector<cpr::Pair> p;
  97. for (auto it = parame.begin(); it != parame.end(); ++it) {
  98. //auto s = fmt::format("{0}", it->first);
  99. //p.AddPair(cpr::Pair{ it->first,it->second }, {});
  100. p.push_back({ it->first,it->second });
  101. }
  102. for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
  103. auto s = fmt::format("{0}/{1}", *it, path.c_str());
  104. cpr::Response r;
  105. if (parame.empty())
  106. {
  107. r = cpr::Get(cpr::Url{ s.c_str() });
  108. }
  109. else {
  110. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ p.begin(),p.end() }, cpr::Header{ {"accept", "application/json"} });
  111. }
  112. if (r.status_code == 200 || r.status_code == 201)
  113. {
  114. return std::move(r.text);
  115. }
  116. else {
  117. m_http_status = r.status_code;
  118. return "";
  119. }
  120. }
  121. return std::string("");
  122. }