我正在使用mingw64。
如何使用Boost Beast lib进行https-request?
我在下面获得了代码,并且可以正常工作,但是我收到了答案: “ 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();
}
答案 0 :(得分:2)
您的流类型为asio::ip::tcp::socket
。它必须为asio::ssl::stream<asio::ip::tcp::socket>
。
文档中的完整示例: