The class read_stream
supports reading content from a specified URL using synchronous or asynchronous
operations.
class read_stream
Member Functions
Name |
Description |
---|---|
Asynchronously opens the specified URL. |
|
Asynchronously reads some data from the stream. |
|
Closes the stream. |
|
Gets the length of the content obtained from the URL. |
|
Gets the MIME type of the content obtained from the URL. |
|
Gets the io_service associated with the stream. |
|
Gets the current value of an option that controls the behaviour of the stream. |
|
Gets the values of all options set on the stream. |
|
Gets the protocol-specific headers obtained from the URL. |
|
Determines whether the stream is open. |
|
Opens the specified URL. |
|
Reads some data from the stream. |
|
Constructs an object of class read_stream. |
|
Sets an option to control the behaviour of the stream. |
|
Sets options to control the behaviour of the stream. |
Remarks
Currently supported URL protocols are http
,
https
and file
.
The class read_stream
meets
the type requirements for SyncReadStream
and AsyncReadStream
,
as defined in the Boost.Asio documentation. This allows objects of class
read_stream
to be used
with the functions boost::asio::read
,
boost::asio::async_read
,
boost::asio::read_until
and boost::asio::async_read_until
.
Example
To synchronously open the URL, read the content and write it to standard output:
try { boost::asio::io_service io_service; urdl::read_stream read_stream(io_service); read_stream.open("http://www.boost.org/LICENSE_1_0.txt"); for (;;) { char data[1024]; boost::system::error_code ec; std::size_t length = stream.read_some(boost::asio::buffer(data), ec); if (ec == boost::asio::error::eof) break; if (ec) throw boost::system::system_error(ec); os.write(data, length); } } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << std::endl; }
To asynchronously open the URL, read the content and write it to standard output:
boost::asio::io_service io_service; urdl::read_stream read_stream(io_service) char data[1024]; ... read_stream.async_open("http://www.boost.org/LICENSE_1_0.txt", open_handler); ... void open_handler(const boost::system::error_code& ec) { if (!ec) { read_stream.async_read_some(boost::asio::buffer(data), read_handler); } } ... void read_handler(const boost::system::error_code& ec, std::size_t length) { if (!ec) { std::cout.write(data, length); read_stream.async_read_some(boost::asio::buffer(data), read_handler); } }
Requirements
Header: <urdl/read_stream.hpp>
Namespace: urdl