DataBuriedPoint.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "DataBuriedPoint.h"
  2. #include <string>
  3. #pragma region 请求数据
  4. HttpRequest::HttpRequest(const std::string& ip, int port) : m_ip(ip), m_port(port)
  5. {
  6. }
  7. HttpRequest::~HttpRequest()
  8. {
  9. CloseSocket();
  10. }
  11. SOCKET HttpRequest::CreateSocket()
  12. {
  13. WSADATA wData;
  14. ::WSAStartup(MAKEWORD(2, 2), &wData);
  15. SOCKET clientSocket = socket(AF_INET, 1, 0);
  16. return clientSocket;
  17. }
  18. int HttpRequest::ConnectSocket(SOCKET socket, std::string ip)
  19. {
  20. struct sockaddr_in ServerAddr = { 0 };
  21. ServerAddr.sin_addr.s_addr = inet_addr(ip.c_str());
  22. ServerAddr.sin_port = htons(m_port);
  23. ServerAddr.sin_family = AF_INET;
  24. //返回值为0,为连接成功
  25. return connect(socket, (sockaddr*)&ServerAddr, sizeof(ServerAddr));
  26. }
  27. int HttpRequest::SendData(SOCKET socket, std::string strSend)
  28. {
  29. //返回值大于0,为发送成功
  30. return send(socket, strSend.c_str(), strSend.length(), 0);
  31. }
  32. void HttpRequest::CloseSocket()
  33. {
  34. ::WSACleanup();
  35. }
  36. std::string HttpRequest::RequesStr(std::string req, std::string data)
  37. {
  38. std::string sRequest =
  39. " HTTP/1.1\r\n"
  40. "Host: ipAddress\r\n"
  41. "Content-Type: application/json\r\n"
  42. "Content-Length: " + std::to_string(data.size()) + "\r\n"
  43. "Connection: close\r\n\r\n" + data;
  44. std::string strSend = "POST " + req + sRequest;
  45. return strSend;
  46. }
  47. std::string HttpRequest::HttpPost(std::string req, std::string data)
  48. {
  49. std::string str = "";
  50. try
  51. {
  52. SOCKET clientSocket = CreateSocket();
  53. int states = ConnectSocket(clientSocket, m_ip);
  54. if (states == 0)
  55. {
  56. std::string strSend = RequesStr(req, data);
  57. states = SendData(clientSocket,strSend);
  58. if (states > 0)
  59. {
  60. }
  61. else
  62. {
  63. CloseSocket();
  64. return "Send Failed";
  65. }
  66. // 接收
  67. char bufRecv[3069] = { 0 };
  68. states = recv(clientSocket, bufRecv, 3069, 0);
  69. CloseSocket();
  70. if (states > 0)
  71. {
  72. str = bufRecv;
  73. }
  74. else
  75. {
  76. return "Recv Failed";
  77. }
  78. }
  79. else
  80. {
  81. states = WSAGetLastError();
  82. }
  83. }
  84. catch (...)
  85. {
  86. CloseSocket();
  87. return "Error";
  88. }
  89. return str;
  90. }
  91. #pragma endregion
  92. BuriedPos::BuriedPos(const std::string& ip, int port)
  93. {
  94. request = new HttpRequest(ip, port);
  95. }
  96. BuriedPos::BuriedPos()
  97. {
  98. request = new HttpRequest("139.196.224.157", 3018);
  99. }
  100. BuriedPos::~BuriedPos(void)
  101. {
  102. delete request;
  103. }
  104. //属于外部调用,返回埋点数据发送给服务端
  105. void BuriedPos::SendBury(std::string key, std::string content)
  106. {
  107. std::string testjson = "{\"data\":{\"subscription\":{\"app_code\":\"com.brother.pdfreaderprofree.windows\"},\"device\":{\"unique_sn\":\"05956BDD6E7C1826657FF4638DB9D9CB\",\"app_version\":\"2.3.9\",\"platform\":\"EXE\",\"language\":\"en - US\",\"time_zone\":\"Ulaanbaatar Standard Time\",\"os\":\"10.0.19044\",\"model\":\"Dell Inc.Inspiron 7591\"}}}";
  108. request->HttpPost("/api/auth_devices/verify", testjson);
  109. }
  110. //服务端对应Json数据格式内容
  111. void BuriedPos::CreateJson()
  112. {
  113. //示例
  114. //根节点
  115. Json::Value root;
  116. //根节点属性
  117. root["name"] = Json::Value("shuiyixin");
  118. root["age"] = Json::Value(21);
  119. root["sex"] = Json::Value("man");
  120. //子节点
  121. Json::Value friends;
  122. //子节点属性
  123. friends["friend_name"] = Json::Value("ZhaoWuxian");
  124. friends["friend_age"] = Json::Value(21);
  125. friends["friend_sex"] = Json::Value("man");
  126. //子节点挂到根节点上
  127. root["friends"] = Json::Value(friends);
  128. //数组形式
  129. root["hobby"].append("sing");
  130. root["hobby"].append("run");
  131. root["hobby"].append("Tai Chi");
  132. auto d = root.toStyledString();
  133. }