我正在尝试创建一个简单的基于 C++ Web 的 GUI。我对使用基于 Qt 或 Visual Studio 的 GUI 不感兴趣。我对基于 web 的东西很感兴趣,因为我的要求非常低和基本。
所以我遇到了基于 C 的 Web 服务器“Mongoose”。在浏览完这些示例后,我编写了一些代码,但它不起作用,因为我对互联网编程的了解几乎为零。我想知道你们是否有一个简单的例子,我可以使用 POST 或 GET 请求从 HTML 表单中检索用户数据。
这是我到目前为止所管理的:
//////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
static const char *s_http_port = "8000";
volatile bool kill_server = FALSE;
struct mg_mgr mgr;
struct mg_connection *nc;
bool control1_triggered = FALSE;
bool control2_triggered = FALSE;
struct file_writer_data {
FILE *fp;
size_t bytes_written;
};
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
printf("Signal received! %d\n", ev);
control1_triggered = TRUE;
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
printf(mp->data.p);
switch (ev) {
case MG_EV_HTTP_PART_DATA:
break;
}
}
static void handle_upload2(struct mg_connection *nc, int ev, void *p) {
printf("Signal received@2! %d\n", ev);
control2_triggered = TRUE;
}
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
(void)ev_data;
switch (ev) {
case MG_EV_HTTP_REQUEST:
// Invoked when the full HTTP request is in the buffer (including body).
mg_printf(nc, "%s",
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"<html><body>Controls"
"<form method=\"GET\" action=\"/upload\" "
" enctype=\"multipart/form-data\">"
"<input type = \"text\" name = \"fname\" value = \"John\">"
"<input type=\"submit\" value=\"Fix Position\" />"
"</form>"
"<form method=\"POST\" action=\"/Kill\" "
" enctype=\"multipart/form-data\">"
"<input type=\"submit\" value=\"Kill Server\" />"
"</form>"
"input.search{width: 20em; height: 2em;}"
"</body></html>");
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
}
int main() {
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_register_http_endpoint(nc, "/upload", handle_upload);
mg_register_http_endpoint(nc, "/Kill", handle_upload2);
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
while (1);
return 0;
}
请注意,我已经在谷歌上搜索了大约 3 天,已经看到了大部分链接和问题。但是对 Mongoose 的支持并不多,您能否帮我举个例子,说明如何使用 Mongoose 解析 GET 或 POST HTML 请求?
太感谢了。
干杯,阿维