mongoose6.18实现http客户端

1. 代码

#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. 代码说明

2.1 响应判断

发送连接使用mg_connect_http_opt,是因为第三个参数struct mg_connect_opts可以向回调函数传一个void*型的变量,用于发送完请求判断是否收到服务的响应,这里传的时struct http_message msg的地址,并将msg.resp_code = -1,这样在循环等待影响时就可以用做while的循环条件。

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

在回调函数中,成功收到服务端的响应后,将ret->resp_code = hm->resp_code赋值为服务端响应码,如果需要获取服务端响应报文,还可以在回调函数中对ret->body进行赋值。

2.2 超时判断

mg_set_timer(nc, mg_time() + 5000);

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

上述代码用于超时判断
mg_set_timer设置超时时间
if (mg_time() >= nc->ev_timer_time)用于超时判断
注意:回调函数中的ev == MG_EV_TIMER并不起作用,不知道为何。

猜你喜欢

转载自blog.csdn.net/wyw0000/article/details/131843697