c++中http服务,收发json格式数据的例子
时间: 2024-02-05 21:14:24 浏览: 98
网上小说采集,提供http api 输出json格式数据。.zip
以下是一个简单的使用C++编写的HTTP服务器,用于接收并处理JSON格式的请求数据。
```c++
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::asio::ip::tcp;
class session
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
boost::asio::async_read_until(socket_, request_, "\r\n\r\n",
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
std::istream request_stream(&request_);
std::string header;
while (std::getline(request_stream, header) && header != "\r")
{
// Handle request headers
}
std::stringstream ss;
ss << request_stream.rdbuf();
std::string body = ss.str();
// Parse JSON
boost::property_tree::ptree pt;
std::istringstream is(body);
boost::property_tree::read_json(is, pt);
// Handle JSON data
std::string response_data = "Received JSON data:\n" + body;
// Construct response
std::string response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: text/plain\r\n";
response += "Content-Length: " + std::to_string(response_data.length()) + "\r\n";
response += "\r\n";
response += response_data;
boost::asio::async_write(socket_, boost::asio::buffer(response),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
delete this;
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
delete this;
}
}
private:
tcp::socket socket_;
boost::asio::streambuf request_;
};
class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
start_accept();
}
private:
void start_accept()
{
session* new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
void handle_accept(session* new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
}
else
{
delete new_session;
}
start_accept();
}
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};
int main()
{
try
{
boost::asio::io_service io_service;
server s(io_service, 8080);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
```
在上面的代码中,我们使用了`boost::asio`库来处理HTTP请求和响应。`session`类表示一个HTTP会话,它处理一个连接。这个类使用了异步编程方式,使用`boost::asio::async_read_until`函数来读取请求数据,然后使用`boost::property_tree`库解析JSON数据。解析完成后,我们可以处理JSON数据并构造响应。最后,我们使用`boost::asio::async_write`函数将响应发送回客户端。
在`server`类中,我们创建一个`tcp::acceptor`对象来监听指定端口的HTTP连接。当有新连接时,我们使用`async_accept`函数接受连接并创建一个新的`session`对象来处理这个连接。为了支持多个连接,我们在`handle_accept`函数中调用`start_accept`函数来接受下一个连接。
请注意,上面的代码仅仅是一个示例,它没有对请求进行任何验证或者错误处理。在实际应用中,您应该根据您的需求来添加适当的验证和错误处理。
阅读全文