ETNoticeManage.cpp
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "stdafx.h"
#include "ETNoticeManage.h"
#include "etradeclient/utility/logon_mgr.h"
#include "etradeclient/utility/win_http.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/application_config.h"
#include "etradeclient/utility/string_converter.h"
#include "etradeclient/utility/session.h"
#include <boost/property_tree/ptree.hpp>
#include "etradeclient/boost_patch/property_tree/json_parser.hpp" // WARNIING! Make sure to include our patched version.
#include <sstream>
CETNoticeManage::CETNoticeManage()
{
}
CETNoticeManage::~CETNoticeManage()
{
}
CString CETNoticeManage::GetNotice()
{
CString sNoticeText;
CString sLog;
LOG_TRACE(_T("开始获取通知消息!"));
const uint32_t kHTTPOK = 200;
//m_err_msg = L"";
auto& cURLConfig = URLConfig::Instance();
WinHttp cWinHttp;
cWinHttp.ConnectHost(cURLConfig.Host(), cURLConfig.Port(), cURLConfig.IsHttps());
auto& cRequest = cWinHttp.OpenRequest(WinHttp::Method::POST, cURLConfig.NoticeMsgPath());
cRequest.SetContentType("Content-Type:application/json;charset=UTF-8");
//std::string post_data("{\"account\":\"" + wstr_2_str(account) + "\"," + "\"password\":\"" + wstr_2_str(pwd) + "\"," + "\"type\":\"" + type + "\"}");
//request.SetPostData(post_data);
if (cURLConfig.IsHttps())
{
auto& cAppConfig = ApplicationConfig::Instance();
cRequest.SetClientCertificate(cAppConfig.ClientCertStore(), cAppConfig.ClientCertSubject());
}
cRequest.Send();
uint32_t iStatusCode = cRequest.GetResponseStatus();
if (kHTTPOK != iStatusCode)
{
sLog.Format(_T("网络请求错误! 错误码: %s"), std::to_wstring(iStatusCode));
LOG_ERROR(sLog.GetBuffer());
}
std::string sResponseBody = cRequest.ReadResponseBody();
if (sResponseBody.empty())
{
LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!");
}
sNoticeText = GetNoticeText(sResponseBody);
return sNoticeText;
}
CString CETNoticeManage::GetNoticeText(std::string sResponse)
{
CString sNoticeText;
namespace PT = boost::property_tree;
try
{
PT::ptree pNoticeTree;
std::stringstream ssNotice;
ssNotice << sResponse;
PT::read_json(ssNotice, pNoticeTree);
PT::ptree pDataTree = pNoticeTree.get_child("data");
CString sID;
int iID = 1;
for (auto index = pDataTree.begin(); index != pDataTree.end(); ++index)
{
sID.Format(_T("%d"), iID);
std::wstring sContent = str_2_wstr(index->second.get<std::string>("content"));
sNoticeText += sID + CString(_T(".")) + CString(sContent.c_str()) + CString(_T(" "));
++iID;
}
}
catch (...)
{
LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
return false;
}
return sNoticeText;
}