dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "EthernetInterface.h"
00004 #include "HTTPClient.h"
00005 
00006 
00007 namespace {
00008     const int BUFFER_SIZE = 512;
00009 }
00010 
00011 int main() {
00012     MBED_HOSTTEST_TIMEOUT(15);
00013     MBED_HOSTTEST_SELECT(default_auto);
00014     MBED_HOSTTEST_DESCRIPTION(HTTP client hello world);
00015     MBED_HOSTTEST_START("NET_7");
00016 
00017     char http_request_buffer[BUFFER_SIZE + 1] = {0};
00018     HTTPClient http;
00019     EthernetInterface eth;
00020     eth.init(); //Use DHCP
00021     eth.connect();
00022 
00023     //GET data
00024     {
00025         bool result = true;
00026         const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt";
00027         printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt);
00028         HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE);
00029         if (ret == HTTP_OK) {
00030             printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer);
00031         } else {
00032             printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
00033             result = false;
00034         }
00035 
00036         if (result == false) {
00037             eth.disconnect();
00038             MBED_HOSTTEST_RESULT(false);
00039         }
00040     }
00041 
00042     //POST data
00043     {
00044         bool result = true;
00045         const char *url_httpbin_post = "http://httpbin.org/post";
00046         HTTPText text(http_request_buffer, BUFFER_SIZE);
00047         HTTPMap map;
00048         map.put("Hello", "World");
00049         map.put("test", "1234");
00050         printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
00051         HTTPResult ret = http.post(url_httpbin_post, map, &text);
00052         if (ret == HTTP_OK) {
00053             printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
00054             printf("HTTP_POST: %s\r\n", http_request_buffer);
00055         } else {
00056             printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
00057             result = false;
00058         }
00059 
00060         if (result == false) {
00061             eth.disconnect();
00062             MBED_HOSTTEST_RESULT(false);
00063         }
00064     }
00065     eth.disconnect();
00066     MBED_HOSTTEST_RESULT(true);
00067 }