Simple HTTP Client Example with HTTPClient class
Dependencies: HTTPClient mbed-rtos mbed-src
main.cpp@3:c98b815a5a99, 2014-12-01 (annotated)
- Committer:
- takashikojo
- Date:
- Mon Dec 01 01:22:19 2014 +0000
- Revision:
- 3:c98b815a5a99
- Parent:
- 2:caac3459ca52
Simple HTTP Client example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:880477b153ac | 1 | #include "mbed.h" |
takashikojo | 0:880477b153ac | 2 | #include "EthernetInterface.h" |
takashikojo | 1:24d373f4f0ea | 3 | #include "HTTPClient.h" |
takashikojo | 0:880477b153ac | 4 | |
takashikojo | 1:24d373f4f0ea | 5 | #define HOST "192.168.1.12" |
takashikojo | 1:24d373f4f0ea | 6 | |
takashikojo | 2:caac3459ca52 | 7 | static HTTPClient http; |
takashikojo | 2:caac3459ca52 | 8 | static char str[1024*20]; |
takashikojo | 1:24d373f4f0ea | 9 | |
takashikojo | 1:24d373f4f0ea | 10 | void main_body(void const *av) { |
takashikojo | 2:caac3459ca52 | 11 | |
takashikojo | 1:24d373f4f0ea | 12 | char url[100] ; |
takashikojo | 1:24d373f4f0ea | 13 | int ret ; |
takashikojo | 1:24d373f4f0ea | 14 | //GET data |
takashikojo | 1:24d373f4f0ea | 15 | |
takashikojo | 1:24d373f4f0ea | 16 | printf("\nTrying to fetch page...\n"); |
takashikojo | 1:24d373f4f0ea | 17 | sprintf(url, "http://%s/", HOST) ; |
takashikojo | 1:24d373f4f0ea | 18 | printf("url=%s\n",url) ; |
takashikojo | 1:24d373f4f0ea | 19 | ret = http.get(url, str, sizeof(str)-1,16); |
takashikojo | 1:24d373f4f0ea | 20 | if (!ret) { |
takashikojo | 1:24d373f4f0ea | 21 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
takashikojo | 1:24d373f4f0ea | 22 | printf("Result: %s\n", str); |
takashikojo | 1:24d373f4f0ea | 23 | } else { |
takashikojo | 1:24d373f4f0ea | 24 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
takashikojo | 0:880477b153ac | 25 | } |
takashikojo | 0:880477b153ac | 26 | |
takashikojo | 1:24d373f4f0ea | 27 | } |
takashikojo | 1:24d373f4f0ea | 28 | |
takashikojo | 2:caac3459ca52 | 29 | static EthernetInterface eth; |
takashikojo | 2:caac3459ca52 | 30 | |
takashikojo | 1:24d373f4f0ea | 31 | int main() |
takashikojo | 1:24d373f4f0ea | 32 | { |
takashikojo | 1:24d373f4f0ea | 33 | int ret ; |
takashikojo | 1:24d373f4f0ea | 34 | void *av ; |
takashikojo | 1:24d373f4f0ea | 35 | printf("HTTP Client Starting,...\n") ; |
takashikojo | 1:24d373f4f0ea | 36 | ret = eth.init(); //Use DHCP |
takashikojo | 1:24d373f4f0ea | 37 | while(1) { |
takashikojo | 1:24d373f4f0ea | 38 | ret = eth.connect(); |
takashikojo | 1:24d373f4f0ea | 39 | if(ret == 0)break ; |
takashikojo | 1:24d373f4f0ea | 40 | } |
takashikojo | 1:24d373f4f0ea | 41 | printf("IP = %s\n", eth.getIPAddress()); |
takashikojo | 2:caac3459ca52 | 42 | |
takashikojo | 1:24d373f4f0ea | 43 | main_body(av) ; |
takashikojo | 2:caac3459ca52 | 44 | |
takashikojo | 1:24d373f4f0ea | 45 | eth.disconnect(); |
takashikojo | 1:24d373f4f0ea | 46 | while(1) { |
takashikojo | 1:24d373f4f0ea | 47 | } |
takashikojo | 1:24d373f4f0ea | 48 | |
takashikojo | 0:880477b153ac | 49 | } |