Francois Berder / Mbed 2 deprecated HTTPSClientExample2

Dependencies:   EthernetInterface7 HTTPSClient_cyassl TLS_cyassl mbed-rtos mbed

Fork of HTTPSClientExample2 by Francois Berder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     This examples shows how to use the HTTPS library 
00003     in order to download a webpage from twitter.com
00004     
00005     Don't forget to download certificates and copy on 
00006     your mbed before running this program. You can 
00007     download them from this link:
00008     http://mbed.org/media/uploads/feb11/certificates-twitter.zip
00009 */
00010  
00011 #include "mbed.h"
00012 #include "EthernetInterface.h"
00013 #include "HTTPSClient.h"
00014  
00015 const char host[] = "twitter.com";
00016 const char request[] = "https://twitter.com";
00017 LocalFileSystem local("local");
00018  
00019 int main()
00020 {
00021     set_time(1378370406);
00022     
00023     /* Starting Ethernet */
00024     EthernetInterface eth;
00025     if(eth.init() || eth.connect())
00026     {
00027         printf("Error with EthernetInterface\n\r");
00028         return -1;
00029     }
00030  
00031     /* Now, let's connect to twitter.com */
00032     HTTPSClient client;
00033     if(!client.connect(host))    
00034     {
00035         printf("Failed to connect to %s\n", host);
00036         return -1;
00037     }
00038     printf("Connected to %s !\n", host);
00039     
00040     /* Let's send our GET request to get the webpage */
00041     char buffer[256];
00042     int bufferLength = sizeof(buffer);
00043     HTTPHeader header;
00044     int read = client.get(request, &header, buffer, bufferLength);
00045     if(header.getStatus() != HTTP_OK || read < 0)
00046     {
00047         printf("Failed sending GET request : %s to %s", request, host);
00048         return -1;
00049     }
00050     
00051     /* index.htm is used to store the webpage */
00052     FILE *fp = fopen("/local/index.htm", "w");
00053     if(fp == NULL)
00054     {
00055         printf("Failed to open file index.htm\n");
00056         return -1;
00057     }
00058     fwrite(buffer, 1, read, fp);   // writing the first part of the body
00059     
00060     int totalRead = read;
00061     while(totalRead < header.getBodyLength())
00062     {
00063         if(bufferLength > header.getBodyLength() - totalRead)
00064             bufferLength = header.getBodyLength() - totalRead;
00065         
00066         /* This function does not send a get request but instead
00067             just receive data from the host.
00068         */
00069         read = client.get("", NULL, buffer, bufferLength);
00070         if(read < 0)
00071         {
00072             printf("Error while getting data from %s\n", host);
00073             return -1;
00074         }
00075         fwrite(buffer, 1, read, fp);
00076         totalRead += read;
00077     }
00078  
00079     /* We're done, let's close everything */
00080     fclose(fp);
00081     printf("Disconnecting from %s\n", host);
00082     client.disconnect();
00083     eth.disconnect();
00084     
00085     return 0;
00086 }