CZhipuziHttpClient.cpp 4.5 KB

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