Mongoose6.18 implements http client

1. Code

#include "mongoose.h"

// Print HTTP response and signal that we're done
static void send_ev_handler(struct mg_connection* c, int ev, void* ev_data) {
    
    
	if (ev == MG_EV_TIMER)
	{
    
    
		printf("timeout...\n");
		struct http_message* hm = (struct http_message*)c->user_data;
		hm->resp_code = 404;
	}
	else if (ev == MG_EV_POLL) {
    
    
		//printf("loopping\n");
	}
	else if (ev == MG_EV_CONNECT) {
    
    
		// Connected to server. Extract host name from URL
		printf("connect successful\n");
	}
	else if (ev == MG_EV_CLOSE) {
    
    
		printf("connect closed\n");
		struct http_message* hm = (struct http_message*)c->user_data;
		hm->resp_code = 200;
	}
	else if (ev == MG_EV_HTTP_REPLY)
	{
    
    
		struct http_message* hm = (struct http_message*)ev_data;
		// 处理HTTP响应
		printf("HTTP Response Code: %d\n", hm->resp_code);
		printf("HTTP Response Body: %.*s\n", (int)hm->body.len, hm->body.p);
		struct http_message* ret = (struct http_message*)c->user_data;
		ret->resp_code = hm->resp_code;
	}
}

static void send_http_request(const char* url, const char* extra_headers, const char* post_data)
{
    
    
	struct mg_mgr mgr;
	struct mg_connection* nc;
	bool done = false;

	mg_mgr_init(&mgr, &done);

	struct http_message msg;
	msg.resp_code = -1;
	struct mg_connect_opts opts;
	memset(&opts, 0, sizeof(opts));
	opts.user_data = &msg;
	nc = mg_connect_http_opt(&mgr, send_ev_handler, opts, url, extra_headers,post_data);
	mg_set_timer(nc, mg_time() + 5000);

	while (msg.resp_code == -1) {
    
    
		mg_mgr_poll(&mgr, 300);
		// 检查定时器是否触发
		if (mg_time() >= nc->ev_timer_time) {
    
    
			printf("connetc timeout\n");
			break;
		}
	}

	mg_mgr_free(&mgr);
}

int main()
{
    
    
	const char json[] = "{\
		\"auto_calc_weight\":1,\
		\"base_camera\" : 1,\
		\"total_cameras\" : 8,\
		\"ctrl_model\" : 5,\
		\"ipc_ip\" : [\
			\"192.168.42.11\",\
			\"192.168.42.12\",\
			\"192.168.42.13\",\
			\"192.168.42.14\",\
			\"192.168.42.15\",\
			\"192.168.42.16\",\
			\"192.168.42.17\",\
			\"192.168.42.18\"\
		]\
}";
	send_http_request("http://192.168.42.11:8880/set_ipc_ctrl_cfg", "Connection: keep-alive\r\nContent-Type: application/json\r\n", json);

	return 0;
}

2. Code Description

2.1 Response judgment

Sending connection uses mg_connect_http_opt, because the third parameter struct mg_connect_opts can pass a variable of void* type to the callback function, which is used to determine whether the response of the service is received after sending the request. Here, the address of struct http_message msg is passed, and the msg.resp_code = -1, so that it can be used as a while loop condition when the loop is waiting for the impact.

while (msg.resp_code == -1) {
    
    
	mg_mgr_poll(&mgr, 300);
	// 检查定时器是否触发
	if (mg_time() >= nc->ev_timer_time) {
    
    
		printf("connetc timeout\n");
		break;
	}
}

In the callback function, after successfully receiving the response from the server, assign ret->resp_code = hm->resp_code as the server response code. If you need to obtain the server response message, you can also set ret->body in the callback function Make an assignment.

2.2 Timeout Judgment

mg_set_timer(nc, mg_time() + 5000);

// 检查定时器是否触发
if (mg_time() >= nc->ev_timer_time) {
    
    
	printf("connetc timeout\n");
	break;
}

The above code is used for timeout judgment
mg_set_timer sets the timeout time
if (mg_time() >= nc->ev_timer_time) is used for timeout judgment
Note: ev == MG_EV_TIMER in the callback function does not work, I don't know why.

Guess you like

Origin blog.csdn.net/wyw0000/article/details/131843697