Simplify http client example

PUBLISHED_FROM=64a91c5d261338c7c224340943d63d4b753ee303
This commit is contained in:
Marko Mikulicic 2016-03-04 17:05:47 +00:00 committed by rojer
parent 0adb080319
commit c82ccd6cf7

View File

@ -6,8 +6,8 @@
#include "mongoose.h"
/* RESTful server host and request URI */
static const char *s_target_address = "ajax.googleapis.com:80";
static const char *s_request = "/ajax/services/search/web?v=1.0&q=cesanta";
static const char *s_url =
"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta";
static int s_exit_flag = 0;
@ -17,14 +17,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch (ev) {
case MG_EV_CONNECT:
connect_status = * (int *) ev_data;
if (connect_status == 0) {
printf("Connected to %s, sending request...\n", s_target_address);
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));
connect_status = *(int *) ev_data;
if (connect_status != 0) {
printf("Error connecting to %s: %s\n", s_url, strerror(connect_status));
s_exit_flag = 1;
}
break;
@ -43,10 +38,10 @@ int main(void) {
struct mg_connection *nc;
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);
printf("Starting RESTful client against %s\n", s_target_address);
printf("Starting RESTful client against %s\n", s_url);
while (s_exit_flag == 0) {
mg_mgr_poll(&mgr, 1000);
}