CZhipuziHttpClient.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if(document.HasMember("errcode"))
  40. {
  41. rapidjson::Value& v_errcode = document["errcode"];
  42. int errcode = v_errcode.GetInt();
  43. if(errcode == -1)
  44. {
  45. LOG_INFO("login failed! message:" << document["errmsg"].GetString());
  46. errmsg = std::string(document["errmsg"].GetString());
  47. return false;
  48. }
  49. }
  50. else if(document.HasMember("error_code"))
  51. {
  52. rapidjson::Value& v_errcode = document["error_code"];
  53. int errcode = v_errcode.GetInt();
  54. if(errcode < 0)
  55. {
  56. LOG_INFO("login failed! message:" << document["error_msg"].GetString());
  57. errmsg = std::string(document["error_msg"].GetString());
  58. return false;
  59. }
  60. }
  61. LOG_INFO("login success!");
  62. return true;
  63. }
  64. bool CZhipuziHttpClient::RequestOld(std::string url, std::map<string, string> params, std::string& response)
  65. {
  66. std::string timestamp = to_string(time(NULL));
  67. std::string nonce = "123456";
  68. //先添加默认参数,用于计算签名
  69. params["machinecode"] = m_client.m_machinecode;
  70. params["username"] = m_client.m_username;
  71. params["nonce"] = nonce;
  72. params["timestamp"] = timestamp;
  73. params["url"] = m_client.m_old_url + url;
  74. //计算签名
  75. std::string postString;
  76. for(std::map<string, string>::iterator it = params.begin(); it != params.end();)
  77. {
  78. postString += it->first + "=" + it->second;
  79. it++;
  80. if(it != params.end())
  81. {
  82. postString += "&";
  83. }
  84. }
  85. LOG_INFO("postString:" << postString.c_str());
  86. //用于计算签名的临时变量
  87. std::string password = md5(m_client.m_password);
  88. transform(password.begin(), password.end(), password.begin(), ::toupper);
  89. string tmp = postString + password;
  90. std::string sign = md5(tmp);
  91. transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
  92. LOG_INFO("sign:" << sign.c_str());
  93. //加上签名,去掉url,计算post
  94. params["sign"] = sign;
  95. params.erase("url");
  96. //未签名之前,不能进行urlencode,签名完成之后可以(params的value必须为utf8格式)
  97. postString = "";
  98. for(std::map<string, string>::iterator it = params.begin(); it != params.end();)
  99. {
  100. postString += it->first + "=" + CLewaimaiString::UrlEncode(it->second);
  101. it++;
  102. if(it != params.end())
  103. {
  104. postString += "&";
  105. }
  106. }
  107. LOG_INFO("postString:" << postString.c_str());
  108. CHttpClient m_httpClient;
  109. int ret = m_httpClient.Posts(m_client.m_old_url + url, postString, response, NULL);
  110. LOG_INFO("response:" << response.c_str());
  111. if(ret == 0)
  112. {
  113. //ret为0表示没有出错
  114. return true;
  115. }
  116. return false;
  117. }
  118. bool CZhipuziHttpClient::RequestNew(std::string url, std::map<string, string> params, std::string& response)
  119. {
  120. std::string timestamp = to_string(time(NULL));
  121. std::string nonce = "123456";
  122. std::string lwm_appid = "84b19199fd221a78c491cd553cbb4ab7";
  123. std::string open_secret = "#repast!@#AfAS#@!";
  124. //先添加默认参数,用于计算签名
  125. params["machinecode"] = m_client.m_machinecode;
  126. params["username"] = CLewaimaiString::UrlEncode(m_client.m_username);
  127. params["password"] = md5(m_client.m_password);
  128. params["lwm_appid"] = "84b19199fd221a78c491cd553cbb4ab7";
  129. params["nonce"] = nonce;
  130. params["timestamp"] = timestamp;
  131. //计算签名
  132. std::string postString;
  133. for(std::map<string, string>::iterator it = params.begin(); it != params.end();)
  134. {
  135. postString += it->first + "=" + it->second;
  136. it++;
  137. if(it != params.end())
  138. {
  139. postString += "&";
  140. }
  141. }
  142. LOG_INFO("postString:" << postString.c_str());
  143. //用于计算签名的临时变量
  144. string tmp = md5(postString);
  145. tmp += open_secret;
  146. std::string sign = md5(tmp);
  147. transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
  148. LOG_INFO("sign:" << sign.c_str());
  149. //加上签名,去掉url,计算post
  150. params["sign"] = sign;
  151. postString = "";
  152. for(std::map<string, string>::iterator it = params.begin(); it != params.end();)
  153. {
  154. postString += it->first + "=" + it->second;
  155. it++;
  156. if(it != params.end())
  157. {
  158. postString += "&";
  159. }
  160. }
  161. LOG_INFO("postString:" << postString.c_str());
  162. CHttpClient m_httpClient;
  163. int ret = m_httpClient.Posts(m_client.m_new_url + url, postString, response, NULL);
  164. LOG_INFO("response:" << response.c_str());
  165. if(ret == 0)
  166. {
  167. //ret为0表示没有出错
  168. return true;
  169. }
  170. return false;
  171. }