C ++ Boost Beast空响应正文

时间:2018-12-17 19:30:20

标签: c++ http boost-asio boost-beast

我正在研究Boost的Beast部分,我正在尝试创建一个简单的http客户端,该客户端可用于 / GET 内容形成Web资源(简单的HTML或REST)服务)。

我已将github repo中的示例用于同步http客户端,并将其移至名为 http_client 的简单类中:
标题:

#ifndef HTTP_CLIENT_HPP
#define HTTP_CLIENT_HPP

#include <string>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>

namespace playground
{
    namespace net
    {

        namespace beast = boost::beast;

        class http_client
        {
        public:
            beast::http::response<beast::http::string_body>
            get(std::string base_url,
                std::string path,
                std::string port = "80",
                unsigned short http_version = 11);
        };

    }
}

#endif

实施:

#include <iostream>
#include <string>
#include <cstdlib>
#include <stdexcept>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>

#include "http_client.hpp"

namespace playground
{
    namespace net
    {

        namespace beast = boost::beast;
        namespace asio = boost::asio;

        beast::http::response<beast::http::string_body> 
        http_client::get(std::string base_url,
                         std::string path,
                         std::string port,
                         unsigned short http_version)
        {
            std::cout << "HttpClient::getAsync()" << std::endl;
            beast::http::response <boost::beast::http::string_body> httpResponse;
            asio::io_context ioContext;
            asio::ip::tcp::resolver tcpResolver
            { ioContext };
            asio::ip::tcp::socket tcpSocket
            { ioContext };
            beast::http::request <beast::http::string_body> httpRequest(beast::http::verb::get, 
                                                                        path, 
                                                                        http_version);
            beast::error_code connectionError;
            beast::error_code readError;
            beast::flat_buffer responseBuffer;

            try
            {

                auto const ipAddresses = tcpResolver.resolve(base_url, port);

                asio::connect(tcpSocket,
                              ipAddresses.begin(),
                              ipAddresses.end());

                if (tcpSocket.is_open())
                {
                    httpRequest.set(beast::http::field::host, base_url);
                    httpRequest.set(beast::http::field::user_agent,
                                    "http_client");
                    httpRequest.set(beast::http::field::accept, "text/xml");

                    beast::http::write(tcpSocket, httpRequest);

                    beast::http::read(tcpSocket,
                                      responseBuffer,
                                      httpResponse,
                                      readError);

                    if (readError != beast::http::error::end_of_stream)
                    {
                        throw std::runtime_error(readError.message());
                    }

                    tcpSocket.shutdown(asio::ip::tcp::socket::shutdown_both,
                                       connectionError);

                    if (connectionError
                            && connectionError != beast::errc::not_connected)
                    {
                        throw beast::system_error(connectionError);
                    }
                }
                else
                {
                    throw std::runtime_error("Unable to open connection!");
                }

            }
            catch (std::exception const& ex)
            {
                throw;
            }
            return httpResponse;
        }

    }
}

现在的问题是,尽管http状态代码为200,但给定的string_body为空。

示例调用:

playground::net::http_client client;
std::string url("www.google.de");
auto response = client.get(url, "/", "443");

std::cout << "Response: " << response << std::endl;
std::cout << "Response body: " << response.body() << std::endl;

通话中是否缺少某些内容?

0 个答案:

没有答案