Connect to twitter.com and copies this webpage to a file.
Dependencies: EthernetInterface3 HTTPSClient TLS_axTLS mbed-rtos mbed
This example shows how to use the HTTPSClient library by copying the content of this webpage to a file. Before running this program, you must download these certificates and copy them to the mbed. After running this program, a file called index.htm has been created and you can open it with your favorite browser to display the webpage the mbed have just downloaded.
Diff: main.cpp
- Revision:
- 1:ff73b1545aaa
- Parent:
- 0:d6829ea0374e
- Child:
- 2:e3807a060fa5
--- a/main.cpp Wed Sep 04 13:40:42 2013 +0000 +++ b/main.cpp Thu Sep 05 10:36:51 2013 +0000 @@ -3,12 +3,13 @@ #include "CertificateManager.h" #include "HTTPSClient.h" -const char host[] = "mbed.org"; -const char request[] = "/"; +const char host[] = "twitter.com"; +const char request[] = "https://twitter.com/"; LocalFileSystem local("local"); int main() { + set_time(1378370406); EthernetInterface eth; if(eth.init() || eth.connect()) { @@ -21,7 +22,6 @@ CertificateManager::add("/local/cert1.der"); CertificateManager::add("/local/cert2.der"); CertificateManager::add("/local/cert3.der"); - CertificateManager::add("/local/cert4.der"); if(!CertificateManager::load(true)) { printf("Failed to load certificates\n"); @@ -34,6 +34,8 @@ printf("Failed to connect to %s\n", host); return -1; } + printf("Connected to %s !\n", host); + CertificateManager::clear(); char buffer[256]; int bufferLength = sizeof(buffer)-1; @@ -45,8 +47,13 @@ return -1; } - buffer[read] ='\0'; - printf("%s", buffer); + FILE *fp = fopen("/local/index.htm", "w"); + if(fp == NULL) + { + printf("Failed to open file index.htm\n"); + return -1; + } + fwrite(buffer, 1, read, fp); int totalRead = read; while(totalRead < header.getBodyLength()) { @@ -54,10 +61,15 @@ bufferLength = header.getBodyLength() - totalRead; read = client.get("", NULL, buffer, bufferLength); - buffer[read] ='\0'; - printf("%s", buffer); + if(read < 0) + { + printf("Error while getting data from %s\n", host); + return -1; + } + fwrite(buffer, 1, read, fp); totalRead += read; } + fclose(fp); printf("Disconnecting from %s\n", host); client.disconnect();