gpt4 book ai didi

c++ - 不能用 boost beast 做 https 请求

转载 作者:行者123 更新时间:2023-11-30 03:17:56 41 4
gpt4 key购买 nike

我正在使用 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

关于c++ - 不能用 boost beast 做 https 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090968/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com