Simple HTTPS access example with HTTPClient class

Dependencies:   EthernetInterface-FRDM HTTPClient mbed-rtos mbed

Committer:
wolfSSL
Date:
Sat Feb 07 19:14:32 2015 +0000
Revision:
4:68c5a3f49a48
Parent:
3:41412e91afb0
SERVER_IP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:dadab10758d2 1 #include "mbed.h"
wolfSSL 0:dadab10758d2 2 #include "EthernetInterface.h"
wolfSSL 0:dadab10758d2 3 #include "HTTPClient.h"
wolfSSL 0:dadab10758d2 4
wolfSSL 0:dadab10758d2 5 EthernetInterface eth;
wolfSSL 0:dadab10758d2 6 HTTPClient http;
wolfSSL 0:dadab10758d2 7 char recvBuff[1024*20];
wolfSSL 0:dadab10758d2 8
wolfSSL 3:41412e91afb0 9 void getline(char *line, int size) {
wolfSSL 3:41412e91afb0 10 for(int i=0; i<size; i++) {
wolfSSL 3:41412e91afb0 11 if((line[i] = getchar()) == '\r') {
wolfSSL 3:41412e91afb0 12 line[i] = '\0' ;
wolfSSL 3:41412e91afb0 13 putchar('\n') ;
wolfSSL 3:41412e91afb0 14 break ;
wolfSSL 3:41412e91afb0 15 } else putchar(line[i]) ;
wolfSSL 3:41412e91afb0 16 }
wolfSSL 3:41412e91afb0 17 }
wolfSSL 3:41412e91afb0 18
wolfSSL 2:071a8275fa40 19 void net_main(void const *av)
wolfSSL 0:dadab10758d2 20 {
wolfSSL 0:dadab10758d2 21 int ret ;
wolfSSL 0:dadab10758d2 22
wolfSSL 0:dadab10758d2 23 eth.init(); //Use DHCP
wolfSSL 0:dadab10758d2 24 printf("HTTP Client, Starting,...\n") ;
wolfSSL 0:dadab10758d2 25 while(1) {
wolfSSL 0:dadab10758d2 26 if(eth.connect() == 0)break ;
wolfSSL 0:dadab10758d2 27 printf("Retry\n") ;
wolfSSL 0:dadab10758d2 28 }
wolfSSL 0:dadab10758d2 29
wolfSSL 0:dadab10758d2 30 /*** HTTP ***/
wolfSSL 3:41412e91afb0 31 printf("\nFetching HTTP\n");
wolfSSL 4:68c5a3f49a48 32 ret = http.get("http://SERVER_IP/574d76fcb/keys.txt", recvBuff, sizeof(recvBuff));
wolfSSL 0:dadab10758d2 33 if (!ret) {
wolfSSL 0:dadab10758d2 34 printf("Result: %s\n", recvBuff);
wolfSSL 0:dadab10758d2 35 } else {
wolfSSL 0:dadab10758d2 36 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
wolfSSL 0:dadab10758d2 37 }
wolfSSL 0:dadab10758d2 38
wolfSSL 0:dadab10758d2 39 /*** HTTPS (SSL) ***/
wolfSSL 3:41412e91afb0 40 printf("\nFetching HTTPS\n");
wolfSSL 4:68c5a3f49a48 41 ret = http.get("https://SERVER_IP/574d76fcb/keys.txt", recvBuff, sizeof(recvBuff));
wolfSSL 0:dadab10758d2 42 if (!ret) {
wolfSSL 0:dadab10758d2 43 printf("Result: %s\n", recvBuff);
wolfSSL 0:dadab10758d2 44 } else {
wolfSSL 0:dadab10758d2 45 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
wolfSSL 0:dadab10758d2 46 }
wolfSSL 0:dadab10758d2 47
wolfSSL 0:dadab10758d2 48 eth.disconnect();
wolfSSL 0:dadab10758d2 49 while(1) {
wolfSSL 0:dadab10758d2 50 }
wolfSSL 2:071a8275fa40 51 }
wolfSSL 2:071a8275fa40 52
wolfSSL 2:071a8275fa40 53 main()
wolfSSL 2:071a8275fa40 54 {
wolfSSL 2:071a8275fa40 55
wolfSSL 2:071a8275fa40 56 #define STACK_SIZE 20000
wolfSSL 2:071a8275fa40 57 Thread t(net_main, NULL, osPriorityNormal, STACK_SIZE);
wolfSSL 2:071a8275fa40 58
wolfSSL 2:071a8275fa40 59 while (true) {
wolfSSL 2:071a8275fa40 60 Thread::wait(1000);
wolfSSL 2:071a8275fa40 61 }
wolfSSL 3:41412e91afb0 62 }