1
1
1HAWK12021-07-04 21:55:14
C++ / C#
1HAWK1, 2021-07-04 21:55:14

How to get data from a website in C++?

How can I get text from a site in c++?
Sample text: https://api.thedogapi.com/

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaliy K, 2021-07-05
@revenger

Here is an example of a c++ application that can make requests and receive responses. Then see for yourself what to do with these answers.

R
res2001, 2021-07-05
@res2001

Just like in any other programming language - send an HTTP request to the site, receive and parse the response. There are many libraries for this, with which this can be done quite easily.
You can do the same without third-party libraries, but it will be much more difficult and you will have to figure everything out yourself.
In the C++ language itself, of course, there are no built-in means for this.

C
cunion, 2021-07-08
@0hquazEd

#include <iostream>
#include <boost/beast.hpp>
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace http = boost::beast::http;

int main(int argc, char** argv)
{
  asio::io_context io;
  asio::ip::tcp::socket socket(io);
  asio::ip::tcp::resolver resolver(io);

  asio::connect(socket, resolver.resolve("scooterlabs.com", "http"));

  http::request<http::empty_body> request(http::verb::get, "/echo", 11);
  request.set(http::field::host, "scooterlabs.com");
  request.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

  http::write(socket, request);

  beast::flat_buffer buffer;
  http::response<http::string_body> response;

  http::read(socket, buffer, response);

  std::cout << response.body() << std::endl;

  socket.shutdown(socket.shutdown_both);
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question