CZhipuziHttpClient.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "../pch/pch.h"
  2. #include "CZhipuziHttpClient.h"
  3. #include "../helper/MD5.h"
  4. CZhipuziHttpClient CZhipuziHttpClient::m_client;
  5. CZhipuziHttpClient::CZhipuziHttpClient()
  6. {
  7. m_username = CSetting::getUsername();
  8. m_password = CSetting::getPassword();
  9. }
  10. CZhipuziHttpClient::~CZhipuziHttpClient()
  11. {
  12. }
  13. void CZhipuziHttpClient::Init(std::string username, std::string password)
  14. {
  15. m_client.m_username = username;
  16. m_client.m_password = password;
  17. }
  18. bool CZhipuziHttpClient::Login(std::string& errmsg)
  19. {
  20. std::map<string, string> params;
  21. std::string response;
  22. bool ret = m_client.RequestNew("/login/login", params, response);
  23. if (!ret)
  24. {
  25. //网络请求出错
  26. LOG_INFO("network failed!");
  27. errmsg = "network failed!";
  28. return false;
  29. }
  30. LOG_INFO("response:" << response.c_str());
  31. rapidjson::Document document;
  32. document.Parse(response.c_str());
  33. if (!document.IsObject())
  34. {
  35. LOG_INFO("message 非法!");
  36. errmsg = "message 非法!";
  37. return false;
  38. }
  39. rapidjson::Value& v_errcode = document["errcode"];
  40. int errcode = v_errcode.GetInt();
  41. if (errcode == -1)
  42. {
  43. LOG_INFO("login failed! message:" << document["errmsg"].GetString());
  44. errmsg = std::string(document["errmsg"].GetString());
  45. return false;
  46. }
  47. LOG_INFO("login success!");
  48. return true;
  49. }
  50. bool CZhipuziHttpClient::RequestOld(std::string url, std::map<string, string> params, std::string& response)
  51. {
  52. std::string timestamp = to_string(time(NULL));
  53. std::string nonce = "123456";
  54. //先添加默认参数,用于计算签名
  55. params["machinecode"] = m_client.m_machinecode;
  56. params["username"] = m_client.m_username;
  57. params["nonce"] = nonce;
  58. params["timestamp"] = timestamp;
  59. params["url"] = m_client.m_old_url + url;
  60. //计算签名
  61. std::string postString;
  62. for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
  63. {
  64. postString += it->first + "=" + it->second;
  65. it++;
  66. if (it != params.end())
  67. {
  68. postString += "&";
  69. }
  70. }
  71. LOG_INFO("postString:" << postString.c_str());
  72. //用于计算签名的临时变量
  73. std::string password = md5(m_client.m_password);
  74. transform(password.begin(), password.end(), password.begin(), ::toupper);
  75. string tmp = postString + password;
  76. std::string sign = md5(tmp);
  77. transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
  78. LOG_INFO("sign:" << sign.c_str());
  79. //加上签名,去掉url,计算post
  80. params["sign"] = sign;
  81. params.erase("url");
  82. postString = "";
  83. for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
  84. {
  85. postString += it->first + "=" + it->second;
  86. it++;
  87. if (it != params.end())
  88. {
  89. postString += "&";
  90. }
  91. }
  92. LOG_INFO("postString:" << postString.c_str());
  93. CHttpClient m_httpClient;
  94. int ret = m_httpClient.Posts(m_client.m_old_url + url, postString, response, NULL);
  95. LOG_INFO("response:" << response.c_str());
  96. if (ret == 0)
  97. {
  98. //ret为0表示没有出错
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool CZhipuziHttpClient::RequestNew(std::string url, std::map<string, string> params, std::string& response)
  104. {
  105. std::string timestamp = to_string(time(NULL));
  106. std::string nonce = "123456";
  107. std::string lwm_appid = "84b19199fd221a78c491cd553cbb4ab7";
  108. std::string open_secret = "#repast!@#AfAS#@!";
  109. //先添加默认参数,用于计算签名
  110. params["machinecode"] = m_client.m_machinecode;
  111. params["username"] = m_client.m_username;
  112. params["password"] = md5(m_client.m_password);
  113. params["lwm_appid"] = "84b19199fd221a78c491cd553cbb4ab7";
  114. params["nonce"] = nonce;
  115. params["timestamp"] = timestamp;
  116. //计算签名
  117. std::string postString;
  118. for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
  119. {
  120. postString += it->first + "=" + it->second;
  121. it++;
  122. if (it != params.end())
  123. {
  124. postString += "&";
  125. }
  126. }
  127. LOG_INFO("postString:" << postString.c_str());
  128. //用于计算签名的临时变量
  129. string tmp = md5(postString);
  130. tmp += open_secret;
  131. std::string sign = md5(tmp);
  132. transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
  133. LOG_INFO("sign:" << sign.c_str());
  134. //加上签名,去掉url,计算post
  135. params["sign"] = sign;
  136. postString = "";
  137. for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
  138. {
  139. postString += it->first + "=" + it->second;
  140. it++;
  141. if (it != params.end())
  142. {
  143. postString += "&";
  144. }
  145. }
  146. LOG_INFO("postString:" << postString.c_str());
  147. CHttpClient m_httpClient;
  148. int ret = m_httpClient.Posts(m_client.m_new_url + url, postString, response, NULL);
  149. LOG_INFO("response:" << response.c_str());
  150. if (ret == 0)
  151. {
  152. //ret为0表示没有出错
  153. return true;
  154. }
  155. return false;
  156. }