blockchain , sdchain cpp sdk and demo

Dependencies:   EthernetInterface mbed-rtos mbed uniqueCPUID

Fork of bcsdk by SDchain C Plus Plus Team

Revision:
12:1b24ea479a59
Parent:
10:aabd720e632c
Child:
14:59412fcf8fa2
--- a/Client.cpp	Wed May 16 12:00:23 2018 +0000
+++ b/Client.cpp	Wed May 16 13:46:01 2018 +0000
@@ -7,10 +7,10 @@
 
 Client::Client()
 {
-	m_strIP = "101.201.199.150";
-	m_sPort = 80;
+	m_strIP = "114.225.164.176";
+	m_sPort = 26990;
 	m_url = "/info";
-	m_host = "testbc.hwelltech.com";
+	m_host = "rest-beta.sdchain.io";
 	m_token = "962cd39d-6496-4b23-a2c5-85e445069a78";
 }
 
@@ -62,8 +62,45 @@
 	return ret;
 }
 
+bool Client::build_delete_raw(const string & url, const string & content, string & post_raw)
+{
+	return build_delete_raw(url, m_host, m_token, content, post_raw);
+}
+
+bool Client::build_delete_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw)
+{
+	bool ret = true;
+
+	post_raw = "";
+	post_raw.append("DELETE " + url + " HTTP/1.1\r\n");
+	post_raw.append("Host: " + host + "\r\n");
+	post_raw.append("Connection: keep-alive\r\n");
+	post_raw.append("accept: application/json\r\n");
+	post_raw.append("content-type: application/json\r\n");
+	string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg==";
+	//if(!token.empty())
+	//	authorization = Base64Encode(token)
+	post_raw.append("authorization: " + authorization + "\r\n");
+
+	char pLen[16] = { 0 };
+	sprintf(pLen,"%d", content.length());
+	string strLen = pLen;
+
+	post_raw.append("Content-Length: " + strLen + "\r\n");
+	post_raw.append("\r\n");
+	post_raw.append(content);
+	post_raw.append("\r\n\r\n");
+
+	return ret;
+}
+
 string Client::get_content(const string & strRep)
 {
+	if (strRep.empty())
+	{
+		cout << "recved string is empty" << endl;
+		return "";
+	}
 	string content = "";
 	content = strRep.substr(strRep.find("\r\n\r\n") + sizeof("\r\n\r\n") - 1); //sizeof("\r\n\r\n") 5
 	if (content.at(0) != '{' && content.at(0) != '[')
@@ -231,3 +268,45 @@
     sClient.close();
 	return 0;
 }
+
+int Client::http_post_delete(const string & url, const string & content, string & strOut)
+{
+	string post_raw = "";
+	build_delete_raw(url, content, post_raw);
+	return http_post_raw(post_raw, strOut);
+}
+
+bool Client::build_get_raw(const string & url, const string & content, string & get_raw)
+{
+	return build_get_raw(url, m_host, m_token, content, get_raw);
+}
+
+bool Client::build_get_raw(const string & url, const string & host, const string & token, const string & content, string & get_raw)
+{
+	bool ret = true;
+
+	get_raw = "";
+	get_raw.append("GET " + url + " HTTP/1.1\r\n");
+	get_raw.append("Host: " + host + "\r\n");
+	get_raw.append("cache-control: no-cache\r\n");
+	get_raw.append("Connection: keep-alive\r\n");
+	get_raw.append("accept: application/json\r\n");
+	get_raw.append("content-type: application/json\r\n");
+	//string token = "c4fa2402-fd65-4ba5-8f9e-4fbb6479612b";
+	get_raw.append("Mbed-Token: " + token + "\r\n");
+	get_raw.append("\r\n\r\n");
+
+	return ret;
+}
+
+int Client::http_get(const string & url, const string & content, string & strOut)
+{
+	string get_raw = "";
+	build_get_raw(url, content, get_raw);
+	return http_post_raw(get_raw, strOut);
+}
+
+int Client::http_get_raw(const string &strIn, string &strOut)
+{
+	return http_post_raw(strIn, strOut);
+}
\ No newline at end of file