[英]Creating a HTTPS request using Boost Asio and OpenSSL
I have created a simple HTTP request wherein I am sending GET,POST and PUT requests to the server.我创建了一个简单的 HTTP 请求,其中我向服务器发送 GET、POST 和 PUT 请求。 Next I want to switch to HTTPS connection using boost asio library, how should I proceed?
接下来我想使用 boost asio 库切换到 HTTPS 连接,我应该如何进行?
I have an Executor Class that resolves and connects to the server and a RequestCreator Class that creates the request.我有一个解析并连接到服务器的执行器 Class 和一个创建请求的 RequestCreator Class。
I happen to just have posted such a thing in a comment (...): 我碰巧在评论中发布了这样的东西(...):
You just connect over ssl.
你只需通过ssl连接。 @Milind coliru.stacked-crooked.com/a/9546326fd1def416
@Milind coliru.stacked-crooked.com/a/9546326fd1def416
So perhaps it is helpful to you. 所以也许对你有帮助。
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
int main() {
boost::system::error_code ec;
using namespace boost::asio;
// what we need
io_service svc;
ssl::context ctx(svc, ssl::context::method::sslv23_client);
ssl::stream<ip::tcp::socket> ssock(svc, ctx);
ssock.lowest_layer().connect({ {}, 8087 }); // http://localhost:8087 for test
ssock.handshake(ssl::stream_base::handshake_type::client);
// send request
std::string request("GET /newGame?name=david HTTP/1.1\r\n\r\n");
boost::asio::write(ssock, buffer(request));
// read response
std::string response;
do {
char buf[1024];
size_t bytes_transferred = ssock.read_some(buffer(buf), ec);
if (!ec) response.append(buf, buf + bytes_transferred);
} while (!ec);
// print and exit
std::cout << "Response received: '" << response << "'\n";
}
To emulate a server for demo purposes I've been using the certificate and params from the Asio samples ( https://stackoverflow.com/a/31201907/85371 ). 为了模拟服务器以进行演示,我一直在使用Asio示例中的证书和参数( https://stackoverflow.com/a/31201907/85371 )。
UPDATE Here's a version that uses resolver to resolve the endpoint (Coliru doesn't allow us to do that, but it does work on non-restricted machines). 更新这是一个使用解析器来解析端点的版本(Coliru不允许我们这样做,但它可以在非限制机器上工作)。
ip::tcp::resolver resolver(svc);
auto it = resolver.resolve({"localhost", "8087"}); // http://localhost:8087 for test
boost::asio::connect(ssock.lowest_layer(), it);
// and the rest unaltered
ssock.handshake(ssl::stream_base::handshake_type::client);
Simple example:
简单示例:
#include<string>
#include<iostream>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
using namespace boost::beast;
using namespace boost::asio;
using std::string;
using std::cout;
int
main() {
const string host = "www.google.com";
const string path = "/";
const string port = "443";
io_service svc;
ssl::context ctx(ssl::context::sslv23_client);
ssl::stream<ip::tcp::socket> ssocket = { svc, ctx };
ip::tcp::resolver resolver(svc);
auto it = resolver.resolve(host, port);
connect(ssocket.lowest_layer(), it);
ssocket.handshake(ssl::stream_base::handshake_type::client);
http::request<http::string_body> req{ http::verb::get, path, 11 };
req.set(http::field::host, host);
http::write(ssocket, req);
http::response<http::string_body> res;
flat_buffer buffer;
http::read(ssocket, buffer, res);
cout << "Headers" << std::endl;
cout << res.base() << std::endl << std::endl;
cout << "Body" << std::endl;
cout << res.body() << std::endl << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.