Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

boost - C++ library to populate SSL stream with valid HTTP requests

I am using Boost.Asio ssl streams, and got a working encrypted socket from which I can send and receive bytes.

I successfully did a GET request with the following code :

// Construct HTTP request (using vanilla std::ostream)
std::ostream request_stream(&request);
request_stream << "GET  / HTTP/1.0
";
request_stream << "Host: " << argv[1] << "
";
...
// Send request
ssl::stream<tcp::socket> socket
boost::asio::write(socket, request);

And I would now love to find a small C++ library that would provide an easy way to get the ostream loaded with a valid HTTP request !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you're already using Boost.Asio, consider using Boost.Beast which is a low-level HTTP library. Example of sending a GET request using Boost.Beast:

using namespace boost::beast;

// Set up an HTTP GET request message
http::request<http::empty_body> req{http::verb::get, "/", 11};
req.set(http::field::host, "www.example.com");
req.set(http::field::user_agent, "Beast/1.0);

// Send the HTTP request to the remote host
http::write(socket, req);

The full example is here: https://github.com/boostorg/beast/blob/master/example/http/client/sync/http_client_sync.cpp

Beast is available in Boost versions 1.66 and later. Here is the documentation page, which includes many examples: http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/index.html

If you really want to write the HTTP request to a std::ostream, Beast supports operator<< for HTTP messages (it is mainly there for debugging), but I think you are better off just writing it directly to the ip::tcp::socket or ssl::stream using Beast.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...