http_client_sync.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. //------------------------------------------------------------------------------
  10. //
  11. // Example: HTTP client, synchronous
  12. //
  13. //------------------------------------------------------------------------------
  14. //[example_http_client
  15. #include <boost/beast/core.hpp>
  16. #include <boost/beast/http.hpp>
  17. #include <boost/beast/version.hpp>
  18. #include <boost/asio/connect.hpp>
  19. #include <boost/asio/ip/tcp.hpp>
  20. #include <cstdlib>
  21. #include <iostream>
  22. #include <string>
  23. namespace beast = boost::beast; // from <boost/beast.hpp>
  24. namespace http = beast::http; // from <boost/beast/http.hpp>
  25. namespace net = boost::asio; // from <boost/asio.hpp>
  26. using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
  27. // Performs an HTTP GET and prints the response
  28. int main(int argc, char** argv)
  29. {
  30. try
  31. {
  32. // Check command line arguments.
  33. if(argc != 4 && argc != 5)
  34. {
  35. std::cerr <<
  36. "Usage: http-client-sync <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
  37. "Example:\n" <<
  38. " http-client-sync www.example.com 80 /\n" <<
  39. " http-client-sync www.example.com 80 / 1.0\n";
  40. return EXIT_FAILURE;
  41. }
  42. auto const host = argv[1];
  43. auto const port = argv[2];
  44. auto const target = argv[3];
  45. int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;
  46. // The io_context is required for all I/O
  47. net::io_context ioc;
  48. // These objects perform our I/O
  49. tcp::resolver resolver(ioc);
  50. beast::tcp_stream stream(ioc);
  51. // Look up the domain name
  52. auto const results = resolver.resolve(host, port);
  53. // Make the connection on the IP address we get from a lookup
  54. stream.connect(results);
  55. // Set up an HTTP GET request message
  56. http::request<http::string_body> req{http::verb::get, target, version};
  57. req.set(http::field::host, host);
  58. req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
  59. // Send the HTTP request to the remote host
  60. http::write(stream, req);
  61. // This buffer is used for reading and must be persisted
  62. beast::flat_buffer buffer;
  63. // Declare a container to hold the response
  64. http::response<http::dynamic_body> res;
  65. // Receive the HTTP response
  66. http::read(stream, buffer, res);
  67. // Write the message to standard out
  68. std::cout << res << std::endl;
  69. // Gracefully close the socket
  70. beast::error_code ec;
  71. stream.socket().shutdown(tcp::socket::shutdown_both, ec);
  72. // not_connected happens sometimes
  73. // so don't bother reporting it.
  74. //
  75. if(ec && ec != beast::errc::not_connected)
  76. throw beast::system_error{ec};
  77. // If we get here then the connection is closed gracefully
  78. }
  79. catch(std::exception const& e)
  80. {
  81. std::cerr << "Error: " << e.what() << std::endl;
  82. return EXIT_FAILURE;
  83. }
  84. return EXIT_SUCCESS;
  85. }
  86. //]