Urdl C++ Library

PrevUpHomeNext

Downloading a file using an istream

The simplest use of Urdl is to download a file or other resource using the urdl::istream class.

// For std::cout and std::cerr.
#include <iostream>
#include <ostream>

// For urdl::istream. Each of Urdl's core classes has its own header file.
#include <urdl/istream.hpp>

int main()
{
  // Open the URL. The connection is established and the HTTP request is sent.
  urdl::istream is("http://www.boost.org/LICENSE_1_0.txt");

  // Check whether we opened the URL successfully.
  if (!is)
  {
    std::cerr << "Unable to open URL" << std::endl;
    return 1;
  }

  // From here on, we can use urdl::istream like any other std::istream
  // object. Let's output the downloaded content one line at a time.
  std::string line;
  while (std::getline(is, line))
  {
    std::cout << line << std::endl;
  }
}

PrevUpHomeNext