我尝试Boost.asio向https://api.binance.com/api/v3/time发出GET请求,它将显示如下结果:
{"serverTime":1679236121308}
为此,我使用了Boost.asio,并从官方Boost库中下载了this example。
看起来我的问题出在握手上。Mi代码看起来像这样:
#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 <cstdlib>
#include <iostream>
#include <string>
namespace http = boost::beast::http;
using namespace std;
using tcp = boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl;
void load_root_certificate(ssl::context& ctx)
{
string cert;
cert += "-----BEGIN CERTIFICATE-----\n"
"MIIGYzCCBUugAwIBAgIQA/p/wyZ8kaMjNHGemaxgFDANBgkqhkiG9w0BAQsFADBe\n"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
"d3cuZGlnaWNlcnQuY29tMR0wGwYDVQQDExRHZW9UcnVzdCBSU0EgQ0EgMjAxODAe\n"
"Fw0yMzAyMDkwMDAwMDBaFw0yNDAyMTYyMzU5NTlaMFsxCzAJBgNVBAYTAktZMREw\n"
"DwYDVQQHEwhXZXN0IEJheTEhMB8GA1UEChMYQmluYW5jZSBIb2xkaW5ncyBMaW1p\n"
"dGVkMRYwFAYDVQQDDA0qLmJpbmFuY2UuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
"AQ8AMIIBCgKCAQEA0vYum8dB5b34k8nB/gKjvryuI5T4kw68zox0h8aRjnhKlS/s\n"
"XjMQRbrUYWAipO4QTBdAwf9R1h+15qUQxpjSGqwkXp3DZ3W27GNLDgXMbjgx2K7n\n"
"KHsNjRvAo+VUtrP976eeuKvYRnqJGWw2V0L0WIV0E6EVI9Mg/+oPoHXcUKwmZ0aM\n"
"h6mEgCvE59IgBv/D131WVe1bTiS4ZoIZnkUy8O+IGbCAo++ubEB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ICIEqhb6LzVrRQNvoRc/q7gFmIKp8OEIX4o7pAwvi+S5kWAHcASLDj\n"
"a9qmRzQP5WoC+p0w6xxSActW3SyB2bu/qznYhHMAAAGGNvxSgQAABAMASDBGAiEA\n"
"9hKsBzUFppDPUhbOlMWdmgvXd27THCnrsQD5kVZiiOkCIQDg925qagt1Xaalo2Hi\n"
"pvCTE0XEYcRKoLere0mQok0C0zANBgkqhkiG9w0BAQsFAAOCAQEAS3J+8Gtp8ss3\n"
"IyvpXPRLy1D1iN0wQ+jDkAM3i0HCBSz2lF0XZi7/UyyB8z7AxqAM8cWjRvVGu3q0\n"
"5e6Jdffj1XQxrdt62WEs+Yl4C1FNPZ2lEL4U2323zeDGkzzpzIDUnaOdvCYZ/2J7\n"
"CAUpFFeSa9YqDaEEwQGOxKqVp92X9rSbFeekqZ3AW5GkSvlMGlnt0zeIKTYX7PRT\n"
"6G7hTs5KNDdzwNYbQY6gWWx3YEMRfoXoDhq4RR5rbjM6RoCXdHKW76QOmrb3rcbF\n"
"VDhusFtvBJw9A1Gyga/Hfh1/xDNfbuzNtBC2lw6ltndhbjeYDuDWYlpIli9Ybp0F\n"
"Y26WPws6rA==\n"
"-----END CERTIFICATE-----";
boost::system::error_code ec;
ctx.add_certificate_authority(boost::asio::buffer(cert.data(), cert.size()), ec);
cout << ec.what() << endl;
}
int main()
{
try {
auto const host = "api.binance.com";
auto port = "80";
auto target = "/api/v3/time";
boost::asio::io_context ioc;
ssl::context ctx{ssl::context::sslv23_client};
ctx.set_default_verify_paths();
load_root_certificate(ctx);
tcp::resolver resolver{ioc};
ssl::stream<tcp::socket> stream{ioc, ctx};
auto const result = resolver.resolve(host, port);
boost::asio::connect(stream.next_layer(), result.begin(), result.end());
stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req{http::verb::get, target, 10};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
boost::beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
cout << res << endl;
boost::system::error_code ec;
stream.shutdown(ec);
if (ec && ec != boost::system::errc::not_connected)
throw boost::system::system_error{ec};
} catch(const exception& e) {
std::cout << "Error: " << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
我用Chrome浏览器在load_toor_certificate函数中添加了证书,但这是我怀疑的部分,因为如果我运行我的代码,我会得到错误:
Error: handshake: packet length too long (SSL routines) [asio.ssl:167772358]
有没有什么方法可以使用boost示例通过HTTPS发出GET或POST请求,而无需下载证书或类似的东西(如CURL)?
PD:使用curl运行相同的查询:
freddy@Freddys-Mac-Studio ~ %
curl https://api.binance.com/api/v3/time
{"serverTime":1679237703479}%
1条答案
按热度按时间5q4ezhmt1#
您使用的是端口80(HTTP),但需要使用端口443(HTTPS),这可能是您从OpenSSL收到一个奇怪错误的原因--因为您没有连接到启用SSL/TLS的端点。