Commit c82ccd6c authored by Marko Mikulicic's avatar Marko Mikulicic Committed by rojer

Simplify http client example

    PUBLISHED_FROM=64a91c5d261338c7c224340943d63d4b753ee303
parent 0adb0803
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include "mongoose.h" #include "mongoose.h"
/* RESTful server host and request URI */ /* RESTful server host and request URI */
static const char *s_target_address = "ajax.googleapis.com:80"; static const char *s_url =
static const char *s_request = "/ajax/services/search/web?v=1.0&q=cesanta"; "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta";
static int s_exit_flag = 0; static int s_exit_flag = 0;
...@@ -17,14 +17,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { ...@@ -17,14 +17,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch (ev) { switch (ev) {
case MG_EV_CONNECT: case MG_EV_CONNECT:
connect_status = * (int *) ev_data; connect_status = *(int *) ev_data;
if (connect_status == 0) { if (connect_status != 0) {
printf("Connected to %s, sending request...\n", s_target_address); printf("Error connecting to %s: %s\n", s_url, strerror(connect_status));
mg_printf(nc, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n",
s_request, s_target_address);
} else {
printf("Error connecting to %s: %s\n",
s_target_address, strerror(connect_status));
s_exit_flag = 1; s_exit_flag = 1;
} }
break; break;
...@@ -43,10 +38,10 @@ int main(void) { ...@@ -43,10 +38,10 @@ int main(void) {
struct mg_connection *nc; struct mg_connection *nc;
mg_mgr_init(&mgr, NULL); mg_mgr_init(&mgr, NULL);
nc = mg_connect(&mgr, s_target_address, ev_handler); nc = mg_connect_http(&mgr, ev_handler, s_url, NULL, NULL);
mg_set_protocol_http_websocket(nc); mg_set_protocol_http_websocket(nc);
printf("Starting RESTful client against %s\n", s_target_address); printf("Starting RESTful client against %s\n", s_url);
while (s_exit_flag == 0) { while (s_exit_flag == 0) {
mg_mgr_poll(&mgr, 1000); mg_mgr_poll(&mgr, 1000);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment