Initial setup for TheKsystem.
Dependencies: mbed-http HTS221
Diff: source/main-https-socket-reuse.cpp
- Branch:
- mbed-os-5.10
- Revision:
- 31:66704f6f17c5
- Parent:
- 30:4825e4f38844
--- a/source/main-https-socket-reuse.cpp Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-https-socket-reuse.cpp Tue Oct 30 11:07:10 2018 +0800
@@ -7,6 +7,7 @@
#if DEMO == DEMO_HTTPS_SOCKET_REUSE
#include "mbed.h"
+#include "mbed_trace.h"
#include "https_request.h"
#include "network-helper.h"
@@ -44,13 +45,13 @@
"-----END CERTIFICATE-----\n";
void dump_response(HttpResponse* res) {
- mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
- mbedtls_printf("Headers:\n");
+ printf("Headers:\n");
for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
- mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
}
- mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+ printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
int main() {
@@ -60,20 +61,30 @@
return 1;
}
+ mbed_trace_init();
+
// Create a TLS socket (which holds a TCPSocket)
printf("\n----- Setting up TLS connection -----\n");
- TLSSocket* socket = new TLSSocket(network, "httpbin.org", 443, SSL_CA_PEM);
- socket->set_debug(true);
- if (socket->connect() != 0) {
- printf("TLS Connect failed %d\n", socket->error());
+ nsapi_error_t r;
+
+ TLSSocket* socket = new TLSSocket();
+ if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
+ printf("TLS socket open failed (%d)\n", r);
+ return 1;
+ }
+ if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
+ printf("TLS socket set_root_ca_cert failed (%d)\n", r);
+ return 1;
+ }
+ if ((r = socket->connect("httpbin.org", 443)) != NSAPI_ERROR_OK) {
+ printf("TLS socket connect failed (%d)\n", r);
return 1;
}
// GET request to httpbin.org
{
HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
- get_req->set_debug(true);
HttpResponse* get_res = get_req->send();
if (!get_res) {
@@ -89,7 +100,6 @@
// POST request to httpbin.org
{
HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post");
- post_req->set_debug(true);
post_req->set_header("Content-Type", "application/json");
const char body[] = "{\"hello\":\"world\"}";
@@ -106,6 +116,7 @@
delete post_req;
}
+ socket->close();
delete socket;
wait(osWaitForever);
Daniel Lee