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