Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface3 HTTPSClient TLS_axTLS mbed-rtos mbed
main.cpp
- Committer:
- feb11
- Date:
- 2013-09-05
- Revision:
- 1:ff73b1545aaa
- Parent:
- 0:d6829ea0374e
- Child:
- 2:e3807a060fa5
File content as of revision 1:ff73b1545aaa:
#include "mbed.h"
#include "EthernetInterface.h"
#include "CertificateManager.h"
#include "HTTPSClient.h"
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())
{
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");
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;
}
printf("Connected to %s !\n", host);
CertificateManager::clear();
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;
}
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())
{
if(bufferLength > header.getBodyLength() - totalRead)
bufferLength = header.getBodyLength() - totalRead;
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);
totalRead += read;
}
fclose(fp);
printf("Disconnecting from %s\n", host);
client.disconnect();
eth.disconnect();
return 0;
}