Networking stack: HTTP Client Streaming example

Dependencies:   mbed

Committer:
donatien
Date:
Thu May 27 11:18:24 2010 +0000
Revision:
1:0e4f1cc78052
Parent:
0:b6b9d1b95cc7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:b6b9d1b95cc7 1 #include "mbed.h"
donatien 0:b6b9d1b95cc7 2 #include "EthernetNetIf.h"
donatien 0:b6b9d1b95cc7 3 #include "HttpClient.h"
donatien 0:b6b9d1b95cc7 4
donatien 0:b6b9d1b95cc7 5 EthernetNetIf eth;
donatien 0:b6b9d1b95cc7 6 HttpClient http;
donatien 0:b6b9d1b95cc7 7
donatien 0:b6b9d1b95cc7 8 HttpResult result;
donatien 0:b6b9d1b95cc7 9 bool completed = false;
donatien 0:b6b9d1b95cc7 10 void request_callback(HttpResult r)
donatien 0:b6b9d1b95cc7 11 {
donatien 0:b6b9d1b95cc7 12 result = r;
donatien 0:b6b9d1b95cc7 13 completed = true;
donatien 0:b6b9d1b95cc7 14 }
donatien 0:b6b9d1b95cc7 15
donatien 0:b6b9d1b95cc7 16 int main() {
donatien 0:b6b9d1b95cc7 17
donatien 0:b6b9d1b95cc7 18 printf("Start\n");
donatien 0:b6b9d1b95cc7 19
donatien 0:b6b9d1b95cc7 20 printf("\r\nSetting up...\r\n");
donatien 0:b6b9d1b95cc7 21 EthernetErr ethErr = eth.setup();
donatien 0:b6b9d1b95cc7 22 if(ethErr)
donatien 0:b6b9d1b95cc7 23 {
donatien 0:b6b9d1b95cc7 24 printf("Error %d in setup.\n", ethErr);
donatien 0:b6b9d1b95cc7 25 return -1;
donatien 0:b6b9d1b95cc7 26 }
donatien 0:b6b9d1b95cc7 27 printf("\r\nSetup OK\r\n");
donatien 0:b6b9d1b95cc7 28
donatien 0:b6b9d1b95cc7 29 HttpStream stream;
donatien 0:b6b9d1b95cc7 30
donatien 1:0e4f1cc78052 31 char BigBuf[512 + 1] = {0};
donatien 1:0e4f1cc78052 32 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
donatien 0:b6b9d1b95cc7 33
donatien 1:0e4f1cc78052 34 HttpResult r = http.get("http://hackaday.com/feed/", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed
donatien 0:b6b9d1b95cc7 35
donatien 0:b6b9d1b95cc7 36 while(!completed)
donatien 0:b6b9d1b95cc7 37 {
donatien 1:0e4f1cc78052 38 Net::poll(); //Polls the Networking stack
donatien 0:b6b9d1b95cc7 39 if(stream.readable())
donatien 0:b6b9d1b95cc7 40 {
donatien 1:0e4f1cc78052 41 BigBuf[stream.readLen()] = 0; //Transform this buffer is zero-terminated char* string
donatien 1:0e4f1cc78052 42 printf("%s",BigBuf); //Displays it while loading
donatien 1:0e4f1cc78052 43 //Note: some server do not like if you throttle them too much, so printf'ing during a request is generally bad practice
donatien 1:0e4f1cc78052 44 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
donatien 0:b6b9d1b95cc7 45 }
donatien 0:b6b9d1b95cc7 46 }
donatien 0:b6b9d1b95cc7 47 printf("\r\n--------------\r\n");
donatien 0:b6b9d1b95cc7 48 if(result == HTTP_OK)
donatien 0:b6b9d1b95cc7 49 {
donatien 0:b6b9d1b95cc7 50 printf("Read completely\n");
donatien 0:b6b9d1b95cc7 51 }
donatien 0:b6b9d1b95cc7 52 else
donatien 0:b6b9d1b95cc7 53 {
donatien 1:0e4f1cc78052 54 printf("Error %d\n", result);
donatien 0:b6b9d1b95cc7 55 }
donatien 0:b6b9d1b95cc7 56
donatien 0:b6b9d1b95cc7 57 while(1)
donatien 0:b6b9d1b95cc7 58 {
donatien 0:b6b9d1b95cc7 59
donatien 0:b6b9d1b95cc7 60 }
donatien 0:b6b9d1b95cc7 61
donatien 0:b6b9d1b95cc7 62 return 0;
donatien 0:b6b9d1b95cc7 63
donatien 0:b6b9d1b95cc7 64 }