8 Mart 2018 Perşembe

beast http write metodu

Giriş
Şu satırı dahil ederiz.
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
Şu satırı dahil ederiz.
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http;    // from <boost/beast/http.hpp>
Belirtilen boost::asio::ip::tcp::socket nesnesini kullanarak beast::http::request nesnesini gönderir. Aslında socket açma ve kapama işleriyle uğaraştırmasaydı çok daha iyi olurdu.

Örnek
Şöyle yaparız.
auto const host = argv[1];
auto const port = argv[2];
auto const target = argv[3];
int version = 11;

// The io_context is required for all I/O
boost::asio::io_context ioc;

// These objects perform our I/O
tcp::resolver resolver{ioc};
tcp::socket socket{ioc};

// Look up the domain name
 auto const results = resolver.resolve(host, port);

// Make the connection on the IP address we get from a lookup
boost::asio::connect(socket, results.begin(), results.end());


// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, version};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

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

// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;

// Declare a container to hold the response
http::response<http::dynamic_body> res;

// Receive the HTTP response
http::read(socket, buffer, res);

// Write the message to standard out
std::cout << res << std::endl;

Hiç yorum yok:

Yorum Gönder