eth test
Dependencies: HTTPClient mbed-rtos
Fork of HTTPClient_HelloWorld by
main.cpp@0:0e0debc29569, 2012-06-29 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 29 11:07:21 2012 +0000
- Revision:
- 0:0e0debc29569
- Child:
- 1:d263603373ac
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
donatien | 0:0e0debc29569 | 2 | #include "EthernetInterface.h" |
donatien | 0:0e0debc29569 | 3 | #include "HTTPClient.h" |
donatien | 0:0e0debc29569 | 4 | |
donatien | 0:0e0debc29569 | 5 | int main() |
donatien | 0:0e0debc29569 | 6 | { |
donatien | 0:0e0debc29569 | 7 | EthernetInterface eth; |
donatien | 0:0e0debc29569 | 8 | eth.init(); //Use DHCP |
donatien | 0:0e0debc29569 | 9 | |
donatien | 0:0e0debc29569 | 10 | eth.connect(); |
donatien | 0:0e0debc29569 | 11 | |
donatien | 0:0e0debc29569 | 12 | //GET data |
donatien | 0:0e0debc29569 | 13 | HTTPClient http; |
donatien | 0:0e0debc29569 | 14 | char str[512]; |
donatien | 0:0e0debc29569 | 15 | printf("Trying to fetch page...\n"); |
donatien | 0:0e0debc29569 | 16 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
donatien | 0:0e0debc29569 | 17 | if (!ret) |
donatien | 0:0e0debc29569 | 18 | { |
donatien | 0:0e0debc29569 | 19 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
donatien | 0:0e0debc29569 | 20 | printf("Result: %s\n", str); |
donatien | 0:0e0debc29569 | 21 | } |
donatien | 0:0e0debc29569 | 22 | else |
donatien | 0:0e0debc29569 | 23 | { |
donatien | 0:0e0debc29569 | 24 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 25 | } |
donatien | 0:0e0debc29569 | 26 | |
donatien | 0:0e0debc29569 | 27 | //POST data |
donatien | 0:0e0debc29569 | 28 | HTTPMap map; |
donatien | 0:0e0debc29569 | 29 | HTTPText text(str, 512); |
donatien | 0:0e0debc29569 | 30 | map.put("Hello", "World"); |
donatien | 0:0e0debc29569 | 31 | map.put("test", "1234"); |
donatien | 0:0e0debc29569 | 32 | printf("Trying to post data...\n"); |
donatien | 0:0e0debc29569 | 33 | ret = http.post("http://httpbin.org/post", map, &text); |
donatien | 0:0e0debc29569 | 34 | if (!ret) |
donatien | 0:0e0debc29569 | 35 | { |
donatien | 0:0e0debc29569 | 36 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
donatien | 0:0e0debc29569 | 37 | printf("Result: %s\n", str); |
donatien | 0:0e0debc29569 | 38 | } |
donatien | 0:0e0debc29569 | 39 | else |
donatien | 0:0e0debc29569 | 40 | { |
donatien | 0:0e0debc29569 | 41 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 42 | } |
donatien | 0:0e0debc29569 | 43 | |
donatien | 0:0e0debc29569 | 44 | eth.disconnect(); |
donatien | 0:0e0debc29569 | 45 | |
donatien | 0:0e0debc29569 | 46 | while(1) { |
donatien | 0:0e0debc29569 | 47 | } |
donatien | 0:0e0debc29569 | 48 | } |