blockchain , sdchain cpp sdk and demo
Dependencies: EthernetInterface mbed-rtos mbed uniqueCPUID
Fork of bcsdk by
Client.cpp@15:7ec76c840343, 2018-06-06 (annotated)
- Committer:
- webmaster
- Date:
- Wed Jun 06 06:14:09 2018 +0000
- Revision:
- 15:7ec76c840343
- Parent:
- 14:59412fcf8fa2
sdchain demo with light
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
webmaster | 8:f2a567ee3a46 | 1 | #include "Client.h" |
webmaster | 8:f2a567ee3a46 | 2 | //#include <WINSOCK2.H> |
webmaster | 8:f2a567ee3a46 | 3 | #include "mbed.h" |
webmaster | 8:f2a567ee3a46 | 4 | #include "EthernetInterface.h" |
webmaster | 8:f2a567ee3a46 | 5 | //#include "BlockChain.h" |
webmaster | 8:f2a567ee3a46 | 6 | |
webmaster | 8:f2a567ee3a46 | 7 | |
webmaster | 8:f2a567ee3a46 | 8 | Client::Client() |
webmaster | 8:f2a567ee3a46 | 9 | { |
webmaster | 15:7ec76c840343 | 10 | m_strIP = "192.168.31.114"; |
MrAI | 14:59412fcf8fa2 | 11 | string strDomain = "hengwell.asuscomm.com"; |
MrAI | 14:59412fcf8fa2 | 12 | getipbydomain(strDomain); |
MrAI | 14:59412fcf8fa2 | 13 | //m_strIP = strDomain; |
webmaster | 15:7ec76c840343 | 14 | m_sPort = 1000; |
webmaster | 8:f2a567ee3a46 | 15 | m_url = "/info"; |
MrAI | 12:1b24ea479a59 | 16 | m_host = "rest-beta.sdchain.io"; |
webmaster | 8:f2a567ee3a46 | 17 | m_token = "962cd39d-6496-4b23-a2c5-85e445069a78"; |
webmaster | 8:f2a567ee3a46 | 18 | } |
webmaster | 8:f2a567ee3a46 | 19 | |
webmaster | 8:f2a567ee3a46 | 20 | Client::Client(string ip,short port) |
webmaster | 8:f2a567ee3a46 | 21 | { |
webmaster | 8:f2a567ee3a46 | 22 | this->m_strIP = ip; |
webmaster | 8:f2a567ee3a46 | 23 | this->m_sPort = port; |
webmaster | 8:f2a567ee3a46 | 24 | |
webmaster | 8:f2a567ee3a46 | 25 | m_url = "/info"; |
webmaster | 8:f2a567ee3a46 | 26 | m_host = "testbc.hwelltech.com"; |
webmaster | 8:f2a567ee3a46 | 27 | m_token = "962cd39d-6496-4b23-a2c5-85e445069a78"; |
webmaster | 8:f2a567ee3a46 | 28 | } |
webmaster | 8:f2a567ee3a46 | 29 | |
webmaster | 8:f2a567ee3a46 | 30 | Client::~Client() |
webmaster | 8:f2a567ee3a46 | 31 | { |
webmaster | 8:f2a567ee3a46 | 32 | |
webmaster | 8:f2a567ee3a46 | 33 | } |
webmaster | 8:f2a567ee3a46 | 34 | |
webmaster | 8:f2a567ee3a46 | 35 | bool Client::build_post_raw(const string & url,const string & content, string & post_raw) |
webmaster | 8:f2a567ee3a46 | 36 | { |
webmaster | 8:f2a567ee3a46 | 37 | return build_post_raw(url,m_host,m_token,content,post_raw); |
webmaster | 8:f2a567ee3a46 | 38 | } |
webmaster | 8:f2a567ee3a46 | 39 | |
webmaster | 8:f2a567ee3a46 | 40 | bool Client::build_post_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw) |
webmaster | 8:f2a567ee3a46 | 41 | { |
webmaster | 8:f2a567ee3a46 | 42 | bool ret = true; |
webmaster | 8:f2a567ee3a46 | 43 | |
webmaster | 8:f2a567ee3a46 | 44 | post_raw = ""; |
webmaster | 8:f2a567ee3a46 | 45 | post_raw.append("POST "+ url + " HTTP/1.1\r\n"); |
webmaster | 8:f2a567ee3a46 | 46 | post_raw.append("Host: " + host + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 47 | post_raw.append("Connection: keep-alive\r\n"); |
webmaster | 8:f2a567ee3a46 | 48 | post_raw.append("accept: application/json\r\n"); |
webmaster | 8:f2a567ee3a46 | 49 | post_raw.append("content-type: application/json\r\n"); |
webmaster | 8:f2a567ee3a46 | 50 | string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg=="; |
webmaster | 8:f2a567ee3a46 | 51 | //if(!token.empty()) |
webmaster | 8:f2a567ee3a46 | 52 | // authorization = Base64Encode(token) |
webmaster | 8:f2a567ee3a46 | 53 | post_raw.append("authorization: " + authorization + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 54 | |
webmaster | 8:f2a567ee3a46 | 55 | char pLen[16] = {0}; |
webmaster | 8:f2a567ee3a46 | 56 | //sprintf_s(pLen, sizeof(pLen), "%d", content.length()); |
webmaster | 8:f2a567ee3a46 | 57 | sprintf(pLen, "%d", content.length()); |
webmaster | 8:f2a567ee3a46 | 58 | string strLen = pLen; |
webmaster | 8:f2a567ee3a46 | 59 | |
webmaster | 8:f2a567ee3a46 | 60 | post_raw.append("Content-Length: " + strLen + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 61 | post_raw.append("\r\n"); |
webmaster | 8:f2a567ee3a46 | 62 | post_raw.append(content); |
webmaster | 8:f2a567ee3a46 | 63 | post_raw.append("\r\n\r\n"); |
webmaster | 8:f2a567ee3a46 | 64 | |
webmaster | 8:f2a567ee3a46 | 65 | return ret; |
webmaster | 8:f2a567ee3a46 | 66 | } |
webmaster | 8:f2a567ee3a46 | 67 | |
MrAI | 12:1b24ea479a59 | 68 | bool Client::build_delete_raw(const string & url, const string & content, string & post_raw) |
MrAI | 12:1b24ea479a59 | 69 | { |
MrAI | 12:1b24ea479a59 | 70 | return build_delete_raw(url, m_host, m_token, content, post_raw); |
MrAI | 12:1b24ea479a59 | 71 | } |
MrAI | 12:1b24ea479a59 | 72 | |
MrAI | 12:1b24ea479a59 | 73 | bool Client::build_delete_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw) |
MrAI | 12:1b24ea479a59 | 74 | { |
MrAI | 12:1b24ea479a59 | 75 | bool ret = true; |
MrAI | 12:1b24ea479a59 | 76 | |
MrAI | 12:1b24ea479a59 | 77 | post_raw = ""; |
MrAI | 12:1b24ea479a59 | 78 | post_raw.append("DELETE " + url + " HTTP/1.1\r\n"); |
MrAI | 12:1b24ea479a59 | 79 | post_raw.append("Host: " + host + "\r\n"); |
MrAI | 12:1b24ea479a59 | 80 | post_raw.append("Connection: keep-alive\r\n"); |
MrAI | 12:1b24ea479a59 | 81 | post_raw.append("accept: application/json\r\n"); |
MrAI | 12:1b24ea479a59 | 82 | post_raw.append("content-type: application/json\r\n"); |
MrAI | 12:1b24ea479a59 | 83 | string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg=="; |
MrAI | 12:1b24ea479a59 | 84 | //if(!token.empty()) |
MrAI | 12:1b24ea479a59 | 85 | // authorization = Base64Encode(token) |
MrAI | 12:1b24ea479a59 | 86 | post_raw.append("authorization: " + authorization + "\r\n"); |
MrAI | 12:1b24ea479a59 | 87 | |
MrAI | 12:1b24ea479a59 | 88 | char pLen[16] = { 0 }; |
MrAI | 12:1b24ea479a59 | 89 | sprintf(pLen,"%d", content.length()); |
MrAI | 12:1b24ea479a59 | 90 | string strLen = pLen; |
MrAI | 12:1b24ea479a59 | 91 | |
MrAI | 12:1b24ea479a59 | 92 | post_raw.append("Content-Length: " + strLen + "\r\n"); |
MrAI | 12:1b24ea479a59 | 93 | post_raw.append("\r\n"); |
MrAI | 12:1b24ea479a59 | 94 | post_raw.append(content); |
MrAI | 12:1b24ea479a59 | 95 | post_raw.append("\r\n\r\n"); |
MrAI | 12:1b24ea479a59 | 96 | |
MrAI | 12:1b24ea479a59 | 97 | return ret; |
MrAI | 12:1b24ea479a59 | 98 | } |
MrAI | 12:1b24ea479a59 | 99 | |
webmaster | 10:aabd720e632c | 100 | string Client::get_content(const string & strRep) |
webmaster | 10:aabd720e632c | 101 | { |
MrAI | 12:1b24ea479a59 | 102 | if (strRep.empty()) |
MrAI | 12:1b24ea479a59 | 103 | { |
MrAI | 12:1b24ea479a59 | 104 | cout << "recved string is empty" << endl; |
MrAI | 12:1b24ea479a59 | 105 | return ""; |
MrAI | 12:1b24ea479a59 | 106 | } |
webmaster | 10:aabd720e632c | 107 | string content = ""; |
webmaster | 10:aabd720e632c | 108 | content = strRep.substr(strRep.find("\r\n\r\n") + sizeof("\r\n\r\n") - 1); //sizeof("\r\n\r\n") 5 |
webmaster | 10:aabd720e632c | 109 | if (content.at(0) != '{' && content.at(0) != '[') |
webmaster | 10:aabd720e632c | 110 | { |
webmaster | 10:aabd720e632c | 111 | content = content.substr(content.find("\r\n") + sizeof("\r\n") - 1); |
webmaster | 10:aabd720e632c | 112 | } |
webmaster | 10:aabd720e632c | 113 | return content; |
webmaster | 10:aabd720e632c | 114 | } |
webmaster | 10:aabd720e632c | 115 | |
webmaster | 10:aabd720e632c | 116 | bool Client::unbuild_post_raw(const string & strRep, int & content_length, string & url,string & content) |
webmaster | 10:aabd720e632c | 117 | { |
webmaster | 10:aabd720e632c | 118 | bool ret = true; |
webmaster | 10:aabd720e632c | 119 | |
webmaster | 10:aabd720e632c | 120 | content_length = get_content_length(strRep); |
webmaster | 10:aabd720e632c | 121 | content = strRep.substr(strRep.find("\r\n\r\n")); |
webmaster | 10:aabd720e632c | 122 | |
webmaster | 10:aabd720e632c | 123 | if (strRep.find("200 OK") < 0) |
webmaster | 10:aabd720e632c | 124 | { |
webmaster | 10:aabd720e632c | 125 | ret = false; |
webmaster | 10:aabd720e632c | 126 | } |
webmaster | 10:aabd720e632c | 127 | |
webmaster | 10:aabd720e632c | 128 | return ret; |
webmaster | 10:aabd720e632c | 129 | } |
webmaster | 10:aabd720e632c | 130 | |
webmaster | 10:aabd720e632c | 131 | int Client::get_content_length(const string & strRaw) |
webmaster | 10:aabd720e632c | 132 | { |
webmaster | 10:aabd720e632c | 133 | int len = -1; // 表示没找到 |
webmaster | 10:aabd720e632c | 134 | /*/ |
webmaster | 10:aabd720e632c | 135 | string strRaw = "\ |
webmaster | 10:aabd720e632c | 136 | HTTP/1.1 200 OK\r\n\ |
webmaster | 10:aabd720e632c | 137 | Blockchain-Id: 13804cbf90e019cc0bcc7243828298492f5d0d87423f776181069eb9af9b2071\r\n\ |
webmaster | 10:aabd720e632c | 138 | Chain-Request-Id: f90965648d6aeb6075e3\r\n\ |
webmaster | 10:aabd720e632c | 139 | Content-Length: 145\r\n\ |
webmaster | 10:aabd720e632c | 140 | X-Frame-Options: DENY\r\n\ |
webmaster | 10:aabd720e632c | 141 | \r\n\r\n\ |
webmaster | 10:aabd720e632c | 142 | {\"alias\":\"t_key_1\",\"xpub\":\"1c926b1e7a88496e5bd7e29736f9ca85c5386bb995c95412b1084ddc1ab5f80e49e7aea51f08b36b3773baa6277c5902f2034d36315b32bf96a3082c048225d5\"}\ |
webmaster | 10:aabd720e632c | 143 | \n"; |
webmaster | 10:aabd720e632c | 144 | */ |
webmaster | 10:aabd720e632c | 145 | string str = ""; |
webmaster | 10:aabd720e632c | 146 | int pos = strRaw.find("Content-Length:") + sizeof("Content-Length:"); |
webmaster | 10:aabd720e632c | 147 | str = strRaw.substr(pos); |
webmaster | 10:aabd720e632c | 148 | pos = str.find("\r\n"); |
webmaster | 10:aabd720e632c | 149 | str = str.substr(0,pos); |
webmaster | 10:aabd720e632c | 150 | len = atoi(str.c_str()); |
webmaster | 10:aabd720e632c | 151 | |
webmaster | 10:aabd720e632c | 152 | return len; |
webmaster | 10:aabd720e632c | 153 | } |
webmaster | 8:f2a567ee3a46 | 154 | int Client::http_post(const string & url,const string & content,string & strOut) |
webmaster | 8:f2a567ee3a46 | 155 | { |
webmaster | 8:f2a567ee3a46 | 156 | string post_raw = ""; |
webmaster | 8:f2a567ee3a46 | 157 | build_post_raw(url,content,post_raw); |
webmaster | 8:f2a567ee3a46 | 158 | return http_post_raw(post_raw,strOut); |
webmaster | 8:f2a567ee3a46 | 159 | } |
webmaster | 8:f2a567ee3a46 | 160 | |
webmaster | 8:f2a567ee3a46 | 161 | int Client::http_post_raw(const string &strIn,string &strOut) |
webmaster | 8:f2a567ee3a46 | 162 | { |
MrAI | 14:59412fcf8fa2 | 163 | cout<<"Client::http_post_raw"<<endl; |
webmaster | 8:f2a567ee3a46 | 164 | string strReq; |
webmaster | 8:f2a567ee3a46 | 165 | string strRep; |
webmaster | 8:f2a567ee3a46 | 166 | strReq = strIn; |
webmaster | 8:f2a567ee3a46 | 167 | |
webmaster | 8:f2a567ee3a46 | 168 | //SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
webmaster | 8:f2a567ee3a46 | 169 | TCPSocketConnection sClient; |
webmaster | 8:f2a567ee3a46 | 170 | /* |
webmaster | 8:f2a567ee3a46 | 171 | if(sClient == INVALID_SOCKET) |
webmaster | 8:f2a567ee3a46 | 172 | { |
webmaster | 8:f2a567ee3a46 | 173 | printf("invalid socket!"); |
webmaster | 8:f2a567ee3a46 | 174 | return 0; |
webmaster | 8:f2a567ee3a46 | 175 | } |
webmaster | 8:f2a567ee3a46 | 176 | */ |
webmaster | 8:f2a567ee3a46 | 177 | #ifdef WIN32 |
webmaster | 8:f2a567ee3a46 | 178 | // windows下是这样设置超时时间 |
webmaster | 8:f2a567ee3a46 | 179 | int recvTimeout = 3 * 1000; //30s |
webmaster | 8:f2a567ee3a46 | 180 | int sendTimeout = 3 * 1000; //30s |
webmaster | 8:f2a567ee3a46 | 181 | setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvTimeout ,sizeof(int)); |
webmaster | 8:f2a567ee3a46 | 182 | setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&sendTimeout ,sizeof(int)); |
webmaster | 8:f2a567ee3a46 | 183 | #elif __LINUX__ |
webmaster | 8:f2a567ee3a46 | 184 | // linux下是这样设置超时时间 |
webmaster | 8:f2a567ee3a46 | 185 | struct timeval timeout = {3,0}; |
webmaster | 8:f2a567ee3a46 | 186 | setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(struct timeval)); |
webmaster | 8:f2a567ee3a46 | 187 | setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(struct timeval)); |
webmaster | 8:f2a567ee3a46 | 188 | #else |
webmaster | 8:f2a567ee3a46 | 189 | unsigned int timeout = 1500; |
webmaster | 8:f2a567ee3a46 | 190 | sClient.set_blocking(false, timeout); // Timeout after (1.5)s |
webmaster | 8:f2a567ee3a46 | 191 | #endif |
webmaster | 8:f2a567ee3a46 | 192 | |
webmaster | 8:f2a567ee3a46 | 193 | |
webmaster | 8:f2a567ee3a46 | 194 | /* |
webmaster | 8:f2a567ee3a46 | 195 | sockaddr_in serAddr; |
webmaster | 8:f2a567ee3a46 | 196 | serAddr.sin_family = AF_INET; |
webmaster | 8:f2a567ee3a46 | 197 | serAddr.sin_port = htons(m_sPort); |
webmaster | 8:f2a567ee3a46 | 198 | serAddr.sin_addr.S_un.S_addr = inet_addr(m_strIP.c_str()); |
webmaster | 8:f2a567ee3a46 | 199 | if(connect(sClient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) |
webmaster | 8:f2a567ee3a46 | 200 | { |
webmaster | 8:f2a567ee3a46 | 201 | printf("connect error !"); |
webmaster | 8:f2a567ee3a46 | 202 | closesocket(sClient); |
webmaster | 8:f2a567ee3a46 | 203 | return SOCKET_ERROR; |
webmaster | 8:f2a567ee3a46 | 204 | } |
webmaster | 8:f2a567ee3a46 | 205 | */ |
MrAI | 14:59412fcf8fa2 | 206 | cout<<"ready to connect to server"<<endl; |
webmaster | 8:f2a567ee3a46 | 207 | while (sClient.connect(m_strIP.c_str(), m_sPort) < 0) { |
webmaster | 8:f2a567ee3a46 | 208 | printf("Unable to connect to (%s) on port (%d)\n", m_strIP.c_str(), m_sPort); |
webmaster | 8:f2a567ee3a46 | 209 | wait(1); |
webmaster | 8:f2a567ee3a46 | 210 | } |
webmaster | 8:f2a567ee3a46 | 211 | printf("Connected to Server at %s\n", m_strIP.c_str()); |
webmaster | 8:f2a567ee3a46 | 212 | |
webmaster | 8:f2a567ee3a46 | 213 | int ret = 0; |
webmaster | 8:f2a567ee3a46 | 214 | |
webmaster | 8:f2a567ee3a46 | 215 | /* |
webmaster | 8:f2a567ee3a46 | 216 | int left = strReq.length(); |
webmaster | 8:f2a567ee3a46 | 217 | unsigned int pos = 0; |
webmaster | 8:f2a567ee3a46 | 218 | ret = send(sClient, strReq.c_str(), strReq.length(), 0); |
webmaster | 8:f2a567ee3a46 | 219 | while (ret < left ) |
webmaster | 8:f2a567ee3a46 | 220 | { |
webmaster | 8:f2a567ee3a46 | 221 | pos = pos + ret; |
webmaster | 8:f2a567ee3a46 | 222 | ret = send(sClient, strReq.c_str() + pos, strReq.length() - pos, 0); |
webmaster | 8:f2a567ee3a46 | 223 | ret = sClient.send(strReq.c_str() + pos, strReq.length() - pos); |
webmaster | 8:f2a567ee3a46 | 224 | if (ret < 0) |
webmaster | 8:f2a567ee3a46 | 225 | { |
webmaster | 8:f2a567ee3a46 | 226 | return SOCKET_ERROR; |
webmaster | 8:f2a567ee3a46 | 227 | } |
webmaster | 8:f2a567ee3a46 | 228 | } |
webmaster | 8:f2a567ee3a46 | 229 | */ |
webmaster | 8:f2a567ee3a46 | 230 | ret = sClient.send_all((char *)strReq.c_str(), strReq.length()); |
webmaster | 8:f2a567ee3a46 | 231 | if (ret < 0) |
webmaster | 8:f2a567ee3a46 | 232 | { |
MrAI | 14:59412fcf8fa2 | 233 | cout<<"send message error"<<endl; |
webmaster | 8:f2a567ee3a46 | 234 | //get last error |
webmaster | 8:f2a567ee3a46 | 235 | //get errno |
webmaster | 8:f2a567ee3a46 | 236 | return ret; |
webmaster | 8:f2a567ee3a46 | 237 | } |
webmaster | 10:aabd720e632c | 238 | #define DEBUG_LOG 1 |
webmaster | 8:f2a567ee3a46 | 239 | #ifdef DEBUG_LOG |
webmaster | 10:aabd720e632c | 240 | //cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl; |
webmaster | 10:aabd720e632c | 241 | printf(">>>>>>>>>>HTTP Request (Length = %d):\r\n%s\r\n", strReq.length(), strReq.c_str()); |
webmaster | 8:f2a567ee3a46 | 242 | #endif |
webmaster | 8:f2a567ee3a46 | 243 | |
webmaster | 8:f2a567ee3a46 | 244 | |
webmaster | 8:f2a567ee3a46 | 245 | char recData[BUFSIZE+1] = {0}; |
webmaster | 8:f2a567ee3a46 | 246 | |
webmaster | 8:f2a567ee3a46 | 247 | ret = 0; |
webmaster | 8:f2a567ee3a46 | 248 | //ret = recv(sClient, recData, BUFSIZE, 0); |
webmaster | 8:f2a567ee3a46 | 249 | ret = sClient.receive(recData, BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 250 | strRep.append(recData); |
webmaster | 8:f2a567ee3a46 | 251 | |
webmaster | 8:f2a567ee3a46 | 252 | // 第一次收数据可能没有收完全,所以一直收到没有数据为止,这里会堵塞 |
webmaster | 8:f2a567ee3a46 | 253 | while (ret > 0) |
webmaster | 8:f2a567ee3a46 | 254 | { |
webmaster | 8:f2a567ee3a46 | 255 | memset(recData,0,BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 256 | //ret = recv(sClient, recData, BUFSIZE, 0); |
webmaster | 8:f2a567ee3a46 | 257 | ret = sClient.receive(recData, BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 258 | |
webmaster | 8:f2a567ee3a46 | 259 | if (ret > 0) |
webmaster | 8:f2a567ee3a46 | 260 | { |
webmaster | 8:f2a567ee3a46 | 261 | strRep.append(recData); |
webmaster | 8:f2a567ee3a46 | 262 | } |
webmaster | 8:f2a567ee3a46 | 263 | } |
webmaster | 8:f2a567ee3a46 | 264 | |
webmaster | 8:f2a567ee3a46 | 265 | strOut = strRep; |
webmaster | 8:f2a567ee3a46 | 266 | |
webmaster | 8:f2a567ee3a46 | 267 | #ifdef DEBUG_LOG |
webmaster | 10:aabd720e632c | 268 | //cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl; |
webmaster | 10:aabd720e632c | 269 | printf(">>>>>>>>>>HTTP Response (Length = %d):\r\n%s\r\n", strRep.length(), strRep.c_str()); |
webmaster | 8:f2a567ee3a46 | 270 | #endif |
webmaster | 8:f2a567ee3a46 | 271 | |
webmaster | 8:f2a567ee3a46 | 272 | // Clean up |
webmaster | 8:f2a567ee3a46 | 273 | //closesocket(sClient); |
webmaster | 8:f2a567ee3a46 | 274 | sClient.close(); |
webmaster | 8:f2a567ee3a46 | 275 | return 0; |
webmaster | 8:f2a567ee3a46 | 276 | } |
MrAI | 12:1b24ea479a59 | 277 | |
MrAI | 12:1b24ea479a59 | 278 | int Client::http_post_delete(const string & url, const string & content, string & strOut) |
MrAI | 12:1b24ea479a59 | 279 | { |
MrAI | 12:1b24ea479a59 | 280 | string post_raw = ""; |
MrAI | 12:1b24ea479a59 | 281 | build_delete_raw(url, content, post_raw); |
MrAI | 12:1b24ea479a59 | 282 | return http_post_raw(post_raw, strOut); |
MrAI | 12:1b24ea479a59 | 283 | } |
MrAI | 12:1b24ea479a59 | 284 | |
MrAI | 12:1b24ea479a59 | 285 | bool Client::build_get_raw(const string & url, const string & content, string & get_raw) |
MrAI | 12:1b24ea479a59 | 286 | { |
MrAI | 12:1b24ea479a59 | 287 | return build_get_raw(url, m_host, m_token, content, get_raw); |
MrAI | 12:1b24ea479a59 | 288 | } |
MrAI | 12:1b24ea479a59 | 289 | |
MrAI | 12:1b24ea479a59 | 290 | bool Client::build_get_raw(const string & url, const string & host, const string & token, const string & content, string & get_raw) |
MrAI | 12:1b24ea479a59 | 291 | { |
MrAI | 14:59412fcf8fa2 | 292 | cout<<"Client::build_get_raw"<<endl; |
MrAI | 12:1b24ea479a59 | 293 | bool ret = true; |
MrAI | 12:1b24ea479a59 | 294 | |
MrAI | 12:1b24ea479a59 | 295 | get_raw = ""; |
MrAI | 12:1b24ea479a59 | 296 | get_raw.append("GET " + url + " HTTP/1.1\r\n"); |
MrAI | 12:1b24ea479a59 | 297 | get_raw.append("Host: " + host + "\r\n"); |
MrAI | 12:1b24ea479a59 | 298 | get_raw.append("cache-control: no-cache\r\n"); |
MrAI | 12:1b24ea479a59 | 299 | get_raw.append("Connection: keep-alive\r\n"); |
MrAI | 12:1b24ea479a59 | 300 | get_raw.append("accept: application/json\r\n"); |
MrAI | 12:1b24ea479a59 | 301 | get_raw.append("content-type: application/json\r\n"); |
MrAI | 12:1b24ea479a59 | 302 | //string token = "c4fa2402-fd65-4ba5-8f9e-4fbb6479612b"; |
MrAI | 12:1b24ea479a59 | 303 | get_raw.append("Mbed-Token: " + token + "\r\n"); |
MrAI | 12:1b24ea479a59 | 304 | get_raw.append("\r\n\r\n"); |
MrAI | 12:1b24ea479a59 | 305 | |
MrAI | 12:1b24ea479a59 | 306 | return ret; |
MrAI | 12:1b24ea479a59 | 307 | } |
MrAI | 12:1b24ea479a59 | 308 | |
MrAI | 12:1b24ea479a59 | 309 | int Client::http_get(const string & url, const string & content, string & strOut) |
MrAI | 12:1b24ea479a59 | 310 | { |
MrAI | 14:59412fcf8fa2 | 311 | cout<<"Client::http_get"<<endl; |
MrAI | 12:1b24ea479a59 | 312 | string get_raw = ""; |
MrAI | 12:1b24ea479a59 | 313 | build_get_raw(url, content, get_raw); |
MrAI | 12:1b24ea479a59 | 314 | return http_post_raw(get_raw, strOut); |
MrAI | 12:1b24ea479a59 | 315 | } |
MrAI | 12:1b24ea479a59 | 316 | |
MrAI | 12:1b24ea479a59 | 317 | int Client::http_get_raw(const string &strIn, string &strOut) |
MrAI | 12:1b24ea479a59 | 318 | { |
MrAI | 12:1b24ea479a59 | 319 | return http_post_raw(strIn, strOut); |
MrAI | 14:59412fcf8fa2 | 320 | } |
MrAI | 14:59412fcf8fa2 | 321 | |
MrAI | 14:59412fcf8fa2 | 322 | bool Client::getipbydomain(string& strDomain) |
MrAI | 14:59412fcf8fa2 | 323 | { |
MrAI | 14:59412fcf8fa2 | 324 | string strIp; |
MrAI | 14:59412fcf8fa2 | 325 | struct hostent *host = gethostbyname(strDomain.c_str()); |
MrAI | 14:59412fcf8fa2 | 326 | for (int i = 0; host->h_addr_list[i]; i++){ |
MrAI | 14:59412fcf8fa2 | 327 | strDomain = (string)inet_ntoa(*(struct in_addr*)host->h_addr_list[i]); |
MrAI | 14:59412fcf8fa2 | 328 | break; |
MrAI | 14:59412fcf8fa2 | 329 | } |
MrAI | 14:59412fcf8fa2 | 330 | return true; |
MrAI | 14:59412fcf8fa2 | 331 | |
MrAI | 12:1b24ea479a59 | 332 | } |