HTTPClient
Some simple demo how to use the HTTPClient.
Library
Import Library:
http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk
Import precompiled Library:
http://mbed.org/projects/cookbook/svn/EMAC/lwip/precomp
HTTP Client Demos
Simple fetching a textfile.
#include "mbed.h"
#include "HTTPClient.h"
DigitalOut led(LED1);
HTTPClient http;
int main(void) {
char result[64];
http.get("http://mbed.org/media/misc/robots.txt", result);
printf("Content is: %s\n", result);
while(1) {
led = !led;
wait(0.2);
}
}
|
| Source Code of mbed-get.bin (LPC1768 LPC2368).
|
Same for a Google search for "HelloWorld":
#include "mbed.h"
#include "HTTPClient.h"
DigitalOut led(LED1);
HTTPClient http;
int main(void) {
char url[256];
char result[2000];
sprintf(url, "http://www.google.co.uk/search?hl=en&q=%s&btnG=Search&meta=", "mbed");
http.get(url, result, 2000);
printf("%s\n", result);
while(1) {
led = !led;
wait(0.2);
}
}
|
| Source Code of google-string.bin (LPC1768 LPC2368).
|
Writing into files
The same HelloWorld search redirected into a file on the local disk:
#include "mbed.h"
#include "HTTPClient.h"
DigitalOut led(LED1);
HTTPClient http;
LocalFileSystem local("local");
int main(void) {
char url[256];
FILE *fd = fopen("/local/hello.htm", "w");
sprintf(url, "http://www.google.co.uk/search?hl=en&q=%s&btnG=Search&meta=", "HelloWorld");
http.get(url, fd);
fclose(fd);
while(1) {
led = !led;
wait(0.2);
}
}
|
| Source Code of google-file.bin (LPC1768 LPC2368).
|
Post nothing
The first demo with a post request:
#include "mbed.h"
#include "HTTPClient.h"
DigitalOut led(LED1);
HTTPClient http;
int main(void) {
char result[64];
http.post("http://mbed.org/media/misc/robots.txt", "", result);
printf("Content is: %s\n", result);
while(1) {
led = !led;
wait(0.2);
}
}
|
| Source Code of mbed-post.bin (LPC1768 LPC1768).
|
API
| HTTPClient | A simple Class to fetch HTTP Pages. |
| Functions | |
| HTTPClient | Creates an HTTPClient object. |
| ~HTTPClient | Destroys the HTTPClient class. |
| headers | Add header additional Information to the next post or get requests. |
| auth | Enables basic authentication. |
| get | A simple get-request just insert the url. |
| get | A simple get-request just insert the url and a FILE Pointer. |
| post | A simple post-request just insert the url. |
| post | A simple get-request just insert the url and a FILE Pointer. |
| post | A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results. |
| post | A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results. |
| timeout | Sets the timout for a HTTP request. |
Resources
- HTTPClient/trunk - Have a look at the API for more demos
- Twitter - or at the twitter example for a more complex example.
- Timetric - or at the timetric example for a more complex example.
Development Log