|
@@ -116,30 +116,52 @@ std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> param
|
|
|
|
|
|
cpr::Response r;
|
|
|
|
|
|
- if (parame.empty())
|
|
|
- {
|
|
|
- r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100});
|
|
|
- }
|
|
|
- else {
|
|
|
- r = cpr::Get(cpr::Url{ s.c_str() }, ps, hander, cpr::Timeout{ 60 * 100 });
|
|
|
- }
|
|
|
- if (r.status_code == 200 || r.status_code == 201)
|
|
|
+
|
|
|
+ std::string res_test = "";
|
|
|
+
|
|
|
+ int count = 3;
|
|
|
+ do
|
|
|
{
|
|
|
- return std::move(r.text);
|
|
|
- }
|
|
|
- else {
|
|
|
- m_http_status = r.status_code;
|
|
|
- if (r.error.message.empty())
|
|
|
+ if (count <= 0)
|
|
|
{
|
|
|
- m_error_msg = r.status_line;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parame.empty())
|
|
|
+ {
|
|
|
+ r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100 });
|
|
|
}
|
|
|
else {
|
|
|
- m_error_msg = r.error.message;
|
|
|
+ r = cpr::Get(cpr::Url{ s.c_str() }, ps, hander, cpr::Timeout{ 60 * 100 });
|
|
|
}
|
|
|
-
|
|
|
- return "";
|
|
|
- }
|
|
|
- return std::string("");
|
|
|
+ if (r.status_code == 200 || r.status_code == 201)
|
|
|
+ {
|
|
|
+ res_test = r.text;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ m_http_status = r.status_code;
|
|
|
+ if (r.error.message.empty())
|
|
|
+ {
|
|
|
+ m_error_msg = r.status_line;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ m_error_msg = UpdateError(r.error.code, r.error.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!res_test.empty())
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ count--;
|
|
|
+
|
|
|
+
|
|
|
+ } while (res_test.empty());
|
|
|
+
|
|
|
+
|
|
|
+ return res_test;
|
|
|
}
|
|
|
|
|
|
std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
|
|
@@ -158,33 +180,81 @@ std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, s
|
|
|
|
|
|
cpr::Response r;
|
|
|
|
|
|
- if (parame.empty())
|
|
|
- {
|
|
|
- r = cpr::Get(cpr::Url{ s.c_str() },hander , cpr::Timeout{ 60 * 100 });
|
|
|
- }
|
|
|
- else {
|
|
|
- r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 100 });
|
|
|
- }
|
|
|
|
|
|
- if (r.status_code == 200 || r.status_code == 201)
|
|
|
+ std::string res_test = "";
|
|
|
+
|
|
|
+ int count = 3;
|
|
|
+ do
|
|
|
{
|
|
|
- return std::move(r.text);
|
|
|
- }
|
|
|
- else {
|
|
|
- m_http_status = r.status_code;
|
|
|
- if (r.error.message.empty())
|
|
|
+ if (count <= 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parame.empty())
|
|
|
{
|
|
|
- m_error_msg = r.status_line;
|
|
|
+ r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 60 * 100 });
|
|
|
}
|
|
|
else {
|
|
|
- m_error_msg = r.error.message;
|
|
|
+ r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 60 * 100 });
|
|
|
}
|
|
|
- return "";
|
|
|
- }
|
|
|
+
|
|
|
+ if (r.status_code == 200 || r.status_code == 201)
|
|
|
+ {
|
|
|
+ res_test = std::move(r.text);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ m_http_status = r.status_code;
|
|
|
+ if (r.error.message.empty())
|
|
|
+ {
|
|
|
+ m_error_msg = r.status_line;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ m_error_msg = UpdateError(r.error.code, r.error.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!res_test.empty())
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ count--;
|
|
|
+
|
|
|
+
|
|
|
+ } while (res_test.empty());
|
|
|
+
|
|
|
+
|
|
|
+ return res_test;
|
|
|
+}
|
|
|
+
|
|
|
+std::string CNetWork::Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token)
|
|
|
+{
|
|
|
+
|
|
|
+ std::string res_test = "";
|
|
|
+
|
|
|
+ int count = 3;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ if (count <= 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ res_test = GetUrl(path, parame, token);
|
|
|
+
|
|
|
+ if (!res_test.empty())
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ count--;
|
|
|
|
|
|
|
|
|
+ } while (res_test.empty());
|
|
|
|
|
|
- return std::string("");
|
|
|
+
|
|
|
+ return res_test;
|
|
|
}
|
|
|
|
|
|
bool write_data(std::string /*data*/, intptr_t /*userdata*/)
|
|
@@ -223,3 +293,30 @@ void CNetWork::Init()
|
|
|
void CNetWork::UnInit()
|
|
|
{
|
|
|
}
|
|
|
+
|
|
|
+std::string CNetWork::UpdateError(cpr::ErrorCode code, std::string msg)
|
|
|
+{
|
|
|
+ if (code == cpr::ErrorCode::HOST_RESOLUTION_FAILURE)
|
|
|
+ {
|
|
|
+ m_error_msg = "解析域名失败";
|
|
|
+ }
|
|
|
+ else if (code == cpr::ErrorCode::OPERATION_TIMEDOUT)
|
|
|
+ {
|
|
|
+ m_error_msg = "请求数据超时,请重新请求";
|
|
|
+ }
|
|
|
+ else if (code == cpr::ErrorCode::CONNECTION_FAILURE) {
|
|
|
+ m_error_msg = "发送服务器失败";
|
|
|
+ }
|
|
|
+ else if (code == cpr::ErrorCode::INVALID_URL_FORMAT) {
|
|
|
+ m_error_msg = "未知URL错误";
|
|
|
+
|
|
|
+ } else if (code == cpr::ErrorCode::SSL_CONNECT_ERROR)
|
|
|
+ {
|
|
|
+ m_error_msg = "SSL连接错误";
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ m_error_msg = msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ return m_error_msg;
|
|
|
+}
|