#include "DataBuriedPoint.h" #include #pragma region 请求数据 HttpRequest::HttpRequest(const std::string& ip, int port) : m_ip(ip), m_port(port) { } HttpRequest::~HttpRequest() { CloseSocket(); } SOCKET HttpRequest::CreateSocket() { WSADATA wData; ::WSAStartup(MAKEWORD(2, 2), &wData); SOCKET clientSocket = socket(AF_INET, 1, 0); return clientSocket; } int HttpRequest::ConnectSocket(SOCKET socket, std::string ip) { struct sockaddr_in ServerAddr = { 0 }; ServerAddr.sin_addr.s_addr = inet_addr(ip.c_str()); ServerAddr.sin_port = htons(m_port); ServerAddr.sin_family = AF_INET; //返回值为0,为连接成功 return connect(socket, (sockaddr*)&ServerAddr, sizeof(ServerAddr)); } int HttpRequest::SendData(SOCKET socket, std::string strSend) { //返回值大于0,为发送成功 return send(socket, strSend.c_str(), strSend.length(), 0); } void HttpRequest::CloseSocket() { ::WSACleanup(); } std::string HttpRequest::RequesStr(std::string req, std::string data) { std::string sRequest = " HTTP/1.1\r\n" "Host: ipAddress\r\n" "Content-Type: application/json\r\n" "Content-Length: " + std::to_string(data.size()) + "\r\n" "Connection: close\r\n\r\n" + data; std::string strSend = "POST " + req + sRequest; return strSend; } std::string HttpRequest::HttpPost(std::string req, std::string data) { std::string str = ""; try { SOCKET clientSocket = CreateSocket(); int states = ConnectSocket(clientSocket, m_ip); if (states == 0) { std::string strSend = RequesStr(req, data); states = SendData(clientSocket,strSend); if (states > 0) { } else { CloseSocket(); return "Send Failed"; } // 接收 char bufRecv[3069] = { 0 }; states = recv(clientSocket, bufRecv, 3069, 0); CloseSocket(); if (states > 0) { str = bufRecv; } else { return "Recv Failed"; } } else { states = WSAGetLastError(); } } catch (...) { CloseSocket(); return "Error"; } return str; } #pragma endregion BuriedPos::BuriedPos(const std::string& ip, int port) { request = new HttpRequest(ip, port); } BuriedPos::BuriedPos() { request = new HttpRequest("139.196.224.157", 3018); } BuriedPos::~BuriedPos(void) { delete request; } //属于外部调用,返回埋点数据发送给服务端 void BuriedPos::SendBury(std::string key, std::string content) { 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\"}}}"; request->HttpPost("/api/auth_devices/verify", testjson); } //服务端对应Json数据格式内容 void BuriedPos::CreateJson() { //示例 //根节点 Json::Value root; //根节点属性 root["name"] = Json::Value("shuiyixin"); root["age"] = Json::Value(21); root["sex"] = Json::Value("man"); //子节点 Json::Value friends; //子节点属性 friends["friend_name"] = Json::Value("ZhaoWuxian"); friends["friend_age"] = Json::Value(21); friends["friend_sex"] = Json::Value("man"); //子节点挂到根节点上 root["friends"] = Json::Value(friends); //数组形式 root["hobby"].append("sing"); root["hobby"].append("run"); root["hobby"].append("Tai Chi"); auto d = root.toStyledString(); }