boost 库 Simple HTTP Client

#include
#include
#include
#include
#include
#include
#include
#include

namespace beast = boost::beast;     // from
namespace http = beast::http;       // from
namespace net = boost::asio;        // from
using tcp = net::ip::tcp;           // from

                  // Performs an HTTP GET and prints the response
int main()
{
    try
    {
        //const char* host = "47.111.173.190";
        //const short port = 8111;
        const std::string host = "127.0.0.1";
        const short port = 8084;
        //std::string target = "api/web/node/server/config";
        std::string target = "/test";
        int version = 11;

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

        // These objects perform our I/O
        tcp::resolver resolver(ioc);
        beast::tcp_stream stream(ioc);

        // Look up the domain name

        // Make the connection on the IP address we get from a lookup
        stream.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(host), port));

        // Set up an HTTP GET request message
        http::request req{ http::verb::get, target, version };
        req.set("Token", "07728ee9faea4f619a563fbb7c15e390");
        req.set("serverId", "257");

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

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

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

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

        // Write the message to standard out
        std::cout << res << std::endl;
        if (res.result_int() == 200) {
            std::string s = boost::beast::buffers_to_string(res.body().data());
        }

        // Gracefully close the socket
        beast::error_code ec;
        stream.socket().shutdown(tcp::socket::shutdown_both, ec);

        // not_connected happens sometimes
        // so don't bother reporting it.
        //
        if (ec && ec != beast::errc::not_connected)
            throw beast::system_error{ ec };

        // If we get here then the connection is closed gracefully
    }
    catch (std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
 

你可能感兴趣的:(boost 库 Simple HTTP Client)