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.
Revision 2:e3807a060fa5, committed 2013-09-05
- Comitter:
- feb11
- Date:
- Thu Sep 05 14:29:02 2013 +0000
- Parent:
- 1:ff73b1545aaa
- Child:
- 3:6d25972a1b21
- Commit message:
- added comments
Changed in this revision
--- a/HTTPSClient.lib Thu Sep 05 10:36:51 2013 +0000 +++ b/HTTPSClient.lib Thu Sep 05 14:29:02 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/feb11/code/HTTPSClient/#95f92eed4e09 +http://mbed.org/users/feb11/code/HTTPSClient/#6d7bc51cc77b
--- a/TLS.lib Thu Sep 05 10:36:51 2013 +0000 +++ b/TLS.lib Thu Sep 05 14:29:02 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/feb11/code/TLS/#26cd1cceb71a +http://mbed.org/users/feb11/code/TLS/#fd7a4452443c
--- a/main.cpp Thu Sep 05 10:36:51 2013 +0000 +++ b/main.cpp Thu Sep 05 14:29:02 2013 +0000 @@ -1,15 +1,27 @@ +/* + This examples shows how to use the HTTPS library + in order to download a webpage from twitter.com + + Don't forget to download certificates and copy on + your mbed before running this program. You can + download them from this link: + http://mbed.org/media/uploads/feb11/certificates-twitter.zip +*/ + #include "mbed.h" #include "EthernetInterface.h" #include "CertificateManager.h" #include "HTTPSClient.h" const char host[] = "twitter.com"; -const char request[] = "https://twitter.com/"; +const char request[] = "https://twitter.com"; LocalFileSystem local("local"); int main() { set_time(1378370406); + + /* Starting Ethernet */ EthernetInterface eth; if(eth.init() || eth.connect()) { @@ -17,8 +29,7 @@ return -1; } - printf("IP address is %s\n\r", eth.getIPAddress()); - + /* Loading certificates in precomputed mode */ CertificateManager::add("/local/cert1.der"); CertificateManager::add("/local/cert2.der"); CertificateManager::add("/local/cert3.der"); @@ -28,6 +39,7 @@ return -1; } + /* Now, let's connect to twitter.com */ HTTPSClient client; if(!client.connect(host)) { @@ -35,10 +47,15 @@ return -1; } printf("Connected to %s !\n", host); + + /* Don't forget to call this method, it's an + easy way to free a few KB of memory */ CertificateManager::clear(); + + /* Let's send our GET request to get the webpage */ char buffer[256]; - int bufferLength = sizeof(buffer)-1; + int bufferLength = sizeof(buffer); HTTPHeader header; int read = client.get(request, &header, buffer, bufferLength); if(header.getStatus() != HTTP_OK || read < 0) @@ -47,30 +64,36 @@ return -1; } + /* index.htm is used to store the webpage */ 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); + fwrite(buffer, 1, read, fp); // writing the first part of the body + int totalRead = read; while(totalRead < header.getBodyLength()) { if(bufferLength > header.getBodyLength() - totalRead) bufferLength = header.getBodyLength() - totalRead; + /* This function does not send a get request but instead + just receive data from the host. + */ read = client.get("", NULL, buffer, bufferLength); if(read < 0) { printf("Error while getting data from %s\n", host); return -1; } - fwrite(buffer, 1, read, fp); + fwrite(buffer, 1, read, fp); totalRead += read; } + + /* We're done, let's close everything */ fclose(fp); - printf("Disconnecting from %s\n", host); client.disconnect(); eth.disconnect();