| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #include "../pch/pch.h"
- #include "CZhipuziHttpClient.h"
- #include "../helper/MD5.h"
- CZhipuziHttpClient CZhipuziHttpClient::m_client;
- CZhipuziHttpClient::CZhipuziHttpClient()
- {
- m_username = CSetting::getUsername();
- m_password = CSetting::getPassword();
- }
- CZhipuziHttpClient::~CZhipuziHttpClient()
- {
- }
- void CZhipuziHttpClient::Init(std::string username, std::string password)
- {
- m_client.m_username = username;
- m_client.m_password = password;
- }
- bool CZhipuziHttpClient::Login(std::string& errmsg)
- {
- std::map<string, string> params;
- std::string response;
- bool ret = m_client.RequestNew("/login/login", params, response);
- if (!ret)
- {
- //网络请求出错
- LOG_INFO("network failed!");
- errmsg = "network failed!";
- return false;
- }
- LOG_INFO("response:" << response.c_str());
- rapidjson::Document document;
- document.Parse(response.c_str());
- if (!document.IsObject())
- {
- LOG_INFO("message 非法!");
- errmsg = "message 非法!";
- return false;
- }
- rapidjson::Value& v_errcode = document["errcode"];
- int errcode = v_errcode.GetInt();
- if (errcode == -1)
- {
- LOG_INFO("login failed! message:" << document["errmsg"].GetString());
- errmsg = std::string(document["errmsg"].GetString());
- return false;
- }
- LOG_INFO("login success!");
-
- return true;
- }
- bool CZhipuziHttpClient::RequestOld(std::string url, std::map<string, string> params, std::string& response)
- {
- std::string timestamp = to_string(time(NULL));
- std::string nonce = "123456";
- //先添加默认参数,用于计算签名
- params["machinecode"] = m_client.m_machinecode;
- params["username"] = m_client.m_username;
- params["nonce"] = nonce;
- params["timestamp"] = timestamp;
- params["url"] = m_client.m_old_url + url;
- //计算签名
- std::string postString;
- for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
- {
- postString += it->first + "=" + it->second;
- it++;
- if (it != params.end())
- {
- postString += "&";
- }
- }
- LOG_INFO("postString:" << postString.c_str());
- //用于计算签名的临时变量
- std::string password = md5(m_client.m_password);
- transform(password.begin(), password.end(), password.begin(), ::toupper);
- string tmp = postString + password;
- std::string sign = md5(tmp);
- transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
- LOG_INFO("sign:" << sign.c_str());
- //加上签名,去掉url,计算post
- params["sign"] = sign;
- params.erase("url");
- postString = "";
- for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
- {
- postString += it->first + "=" + it->second;
- it++;
- if (it != params.end())
- {
- postString += "&";
- }
- }
- LOG_INFO("postString:" << postString.c_str());
- CHttpClient m_httpClient;
- int ret = m_httpClient.Posts(m_client.m_old_url + url, postString, response, NULL);
- LOG_INFO("response:" << response.c_str());
- if (ret == 0)
- {
- //ret为0表示没有出错
- return true;
- }
- return false;
- }
- bool CZhipuziHttpClient::RequestNew(std::string url, std::map<string, string> params, std::string& response)
- {
- std::string timestamp = to_string(time(NULL));
- std::string nonce = "123456";
- std::string lwm_appid = "84b19199fd221a78c491cd553cbb4ab7";
- std::string open_secret = "#repast!@#AfAS#@!";
- //先添加默认参数,用于计算签名
- params["machinecode"] = m_client.m_machinecode;
- params["username"] = m_client.m_username;
- params["password"] = md5(m_client.m_password);
- params["lwm_appid"] = "84b19199fd221a78c491cd553cbb4ab7";
- params["nonce"] = nonce;
- params["timestamp"] = timestamp;
- //计算签名
- std::string postString;
- for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
- {
- postString += it->first + "=" + it->second;
- it++;
- if (it != params.end())
- {
- postString += "&";
- }
- }
- LOG_INFO("postString:" << postString.c_str());
- //用于计算签名的临时变量
- string tmp = md5(postString);
- tmp += open_secret;
- std::string sign = md5(tmp);
- transform(sign.begin(), sign.end(), sign.begin(), ::toupper);
- LOG_INFO("sign:" << sign.c_str());
- //加上签名,去掉url,计算post
- params["sign"] = sign;
- postString = "";
- for (std::map<string, string>::iterator it = params.begin(); it != params.end(); )
- {
- postString += it->first + "=" + it->second;
- it++;
- if (it != params.end())
- {
- postString += "&";
- }
- }
- LOG_INFO("postString:" << postString.c_str());
- CHttpClient m_httpClient;
- int ret = m_httpClient.Posts(m_client.m_new_url + url, postString, response, NULL);
- LOG_INFO("response:" << response.c_str());
- if (ret == 0)
- {
- //ret为0表示没有出错
- return true;
- }
- return false;
- }
|