不能用 boost beast 做 https 请求

Can't do https-request with boost beast

我正在使用 mingw64。

如何使用 boost beast lib 发出 https 请求?

我得到了下面的代码并且它有效,但我收到了答案: “400 纯 HTTP 请求已发送到 HTTPS 端口

发起https请求需要填写什么?已安装 OpenSSL。

#include <boost/beast/root_certificates.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <iostream>

void boost_request()
{
    namespace http = boost::beast::http;
    const std::string host = "api.vk.com"; //"scooterlabs.com";
    const std::string target = "/method/wall.get?&owner_id=-***&access_token=***&v=5.61&count=1000"; //"/echo?input=test";

    boost::asio::io_context ioc;
    boost::asio::ip::tcp::resolver resolver(ioc);
    boost::asio::ip::tcp::socket socket(ioc);
    boost::asio::ip::tcp::resolver::query query("api.vk.com", "https");
    boost::asio::connect(socket.lowest_layer(), resolver.resolve(query));
    socket.lowest_layer().set_option(boost::asio::ip::tcp::no_delay(true));
    http::request<http::string_body> req(http::verb::get, target, 11);
    req.set(http::field::host, host);
    req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
    http::write(socket, req);
    boost::beast::flat_buffer buffer;
    http::response<http::dynamic_body> res;
    http::read(socket, buffer, res);
    std::cout << res << std::endl;
    socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
}

int main()
{
    boost_request();
}

您的直播类型是asio::ip::tcp::socket。必须是 asio::ssl::stream<asio::ip::tcp::socket>.

文档中的完整示例:

https://www.boost.org/doc/libs/1_69_0/libs/beast/example/http/client/sync-ssl/http_client_sync_ssl.cpp