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.

main.cpp

Committer:
feb11
Date:
2013-09-04
Revision:
0:d6829ea0374e
Child:
1:ff73b1545aaa

File content as of revision 0:d6829ea0374e:

#include "mbed.h"
#include "EthernetInterface.h"
#include "CertificateManager.h"
#include "HTTPSClient.h"

const char host[] = "mbed.org";
const char request[] = "/";
LocalFileSystem local("local");

int main()
{
    EthernetInterface eth;
    if(eth.init() || eth.connect())
    {
        printf("Error with EthernetInterface\n\r");
        return -1;
    }

    printf("IP address is %s\n\r", eth.getIPAddress());

    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");
        return -1;
    }

    HTTPSClient client;
    if(!client.connect(host))    
    {
        printf("Failed to connect to %s\n", host);
        return -1;
    }
    
    char buffer[256];
    int bufferLength = sizeof(buffer)-1;
    HTTPHeader header;
    int read = client.get(request, &header, buffer, bufferLength);
    if(header.getStatus() != HTTP_OK || read < 0)
    {
        printf("Failed sending GET request : %s to %s", request, host);
        return -1;
    }
    
    buffer[read] ='\0';
    printf("%s", buffer);
    int totalRead = read;
    while(totalRead < header.getBodyLength())
    {
        if(bufferLength > header.getBodyLength() - totalRead)
            bufferLength = header.getBodyLength() - totalRead;
        
        read = client.get("", NULL, buffer, bufferLength);
        buffer[read] ='\0';
        printf("%s", buffer);
        totalRead += read;
    }
    
    printf("Disconnecting from %s\n", host);
    client.disconnect();
    eth.disconnect();
    
    return 0;
}