123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "pch.h"
- #include "CNetwork.h"
- #include <nlohmann/json.hpp>
- CNetwork::CNetwork() : m_http_ret(http_no), m_http_status(0)
- {
- Init();
- }
- CNetwork::~CNetwork(void)
- {
- UnInit();
- }
-
- //void CNetwork::GetServerlist()
- //{
- // keymap.clear();
- // std::string path = "v1/nodeproess";
- // m_http_ret = http_start;
- // auto data = GetUrl(path, keymap);
- // if (data.empty())
- // {
- // m_http_ret = http_f;
- // m_error_msg = "";
- // m_error_msg.append("请求错误:网络问题,错误码:").append(std::to_string(m_http_status));
- // return;
- // }
- // //解析数据
- // /*if (!m_server_list)
- // {
- // m_error_msg = "server_list为空";
- // return;
- // }
- //
- //
- // if (!m_server_list->parse(data))
- // {
- // m_error_msg = m_server_list->GetLastErrorA();
- // m_http_ret = http_f;
- // return;
- // }*/
- //
- //
- // m_http_ret = http_end;
- //}
-
-
- void CNetwork::Init() {
-
- }
- void CNetwork::UnInit()
- {
- /*if (m_server_list)
- {
- delete m_server_list;
- m_server_list = NULL;
- }
- if (m_userinfo)
- {
- delete m_userinfo;
- m_userinfo = NULL;
- }*/
- }
-
-
- HTTPRET CNetwork::GetHttpConnectstatus()
- {
- return m_http_ret;
- }
- LPCSTR CNetwork::GetLastErrorA()
- {
- return m_error_msg.c_str();
- }
-
- std::string CNetwork::GetUrl(std::string path,std::unordered_map<std::string, std::string> parame)
- {
-
- cpr::Parameters p;
- for (auto it = parame.begin(); it != parame.end(); ++it) {
- //auto s = fmt::format("{0}", it->first);
- /*p.AddParameter({ it->first, it->second }, {});*/
- }
- for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
- auto s = fmt::format("{0}/{1}", *it, path.c_str());
-
- cpr::Response r;
- if (parame.empty())
- {
- r = cpr::Get(cpr::Url{ s.c_str() });
- }
- else {
- r = cpr::Get(cpr::Url{ s.c_str() }, p,cpr::Header{ {"accept", "application/json"} });
- }
-
- if (r.status_code == 200 || r.status_code == 201)
- {
- return r.text;
- }
- else {
- m_http_status = r.status_code;
- return "";
- }
- }
-
- return std::string("");
- }
- std::string CNetwork::PostUrl(std::string path,std::unordered_map<std::string, std::string> parame, std::string data)
- {
- std::vector<cpr::Pair> p;
- for (auto it = parame.begin(); it != parame.end(); ++it) {
- //auto s = fmt::format("{0}", it->first);
- //p.AddPair(cpr::Pair{ it->first,it->second }, {});
- p.push_back({ it->first,it->second });
- }
- for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
- auto s = fmt::format("{0}/{1}", *it, path.c_str());
- cpr::Response r;
- if (parame.empty())
- {
- r = cpr::Get(cpr::Url{ s.c_str() });
- }
- else {
- r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ p.begin(),p.end() }, cpr::Header{ {"accept", "application/json"} });
- }
- if (r.status_code == 200 || r.status_code == 201)
- {
- return std::move(r.text);
- }
- else {
- m_http_status = r.status_code;
- return "";
- }
- }
- return std::string("");
- }
|