mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-25 03:29:00 +08:00
Simplify http client example
PUBLISHED_FROM=64a91c5d261338c7c224340943d63d4b753ee303
This commit is contained in:
parent
0adb080319
commit
c82ccd6cf7
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user