我希望一个网站将数据发送到数据库中。因此,我认为我需要Web服务器,收听HTTP,解析帖子,提取数据并存储到数据库中?
我希望Web服务器为C ++。
但是,我正在努力找到一个简单的例子。我认为这很简单,因为我们只是在听TCP插座,一旦收到了所有数据,那只是一个琴弦解析练习吗?我发现的所有示例似乎都很复杂。例如:
肯定应该不需要300-400行即字符串?
我的理解是否错误?是否有一种简单的方法来处理C ++中的HTTP帖子?
I would like a website to send data to be stored to a database. Consequently I think I require a web server, listening to HTTP, parsing the POST, extracting the data and storing to database?
I'd like the web server to be C++.
However, I am struggling to find a simple example. I assume this is simple because we're just listening to a TCP socket and once all the data has been received, it's just a string parsing exercise? All the examples I can find seem way-over complicated. For example:
https://www.boost.org/doc/libs/1_72_0/libs/beast/example/http/server/async/http_server_async.cpp
Surely it shouldn't require 300-400 lines just to listen to a socket and parse a string?
Is my understanding wrong/is there a simple way to process a HTTP POST in C++?
发布评论
评论(2)
在C ++中编写您自己的Web服务器,该
不会太困难。但是,如果您希望您的Web服务器完全 htttp/1 https://en.wikipedia.org/wiki/http/2“ rel =“ nofollow noreferrer”> http/2 或 http/3 合规,然后编写这样的服务器将是很多工作,因为这些标准是数十个甚至数百页。
使用现有的http服务器软件可能更有意义编写自己的程序,该程序旨在服务单个HTTP请求,并配置服务器以调用某些URL的这些程序。这样,您就不必处理HTTP的大多数详细信息,但是您仍然必须处理HTTP请求和HTTP响应。
一些脚本语言,例如 php (旨在在Web服务器上运行)其余的HTTP详细信息为您提供,因此您不必担心解释HTTP请求标头并创建HTTP响应标头。这些脚本语言通常还允许您连接到外部数据库服务器。
如果您不想使用脚本语言,但更喜欢通过C ++编写的程序来处理单个的HTTP请求,那么这也是可能的,例如使用
Writing your own web server in C++ that
would not be too hard. However, if you want your web server to be fully HTTP/1, HTTP/2 or HTTP/3 compliant, then writing such a server would be a lot of work, as those standards are dozens or even hundreds of pages long.
It would probably be more meaningful to use existing HTTP server software, such as Apache HTTP Server, and write your own programs that are designed to serve individual HTTP requests, and to configure the server to call these programs for certain URLs. That way, you won't have to deal with most of the details of HTTP, but you will still have to deal with HTTP requests and HTTP responses.
Some scripting languages such as PHP (which is designed to run on a web server) handle most of the remaining HTTP details for you, so you for example don't have to worry much about interpreting the HTTP request header and creating a HTTP response header. These scripting languages generally also allow you to connect to an external database server.
If you don't want to use a scripting language, but would prefer for the individual HTTP requests to be handled by programs written in C++, then this is possible too, for example using CGI or an API provided by the HTTP server software.
我建议您使用与C ++ 14一起使用的休息库。在像您这样的情况下非常易于使用。这取决于您的字符串,但我想您可以用最多50行解决您的问题。使用此库,很容易处理发布请求。您可以将处理程序作为一个函数实现,并将其作为方法处理程序提供给库。库的文档:
I recommend you to use restbed library which is work with c++14. Very easy to use in cases like yours. It is very up to your string but I guess you can solve your issue with 50 lines most. With this library it is very easy to handle post requests. You can implement your handlers as a function and give them to library as a method handler. Documentation of library : https://github.com/Corvusoft/restbed/blob/master/documentation/API.md?ysclid=l4ny6wicl231191828