|
| 1 | +.. _atom_reader: |
| 2 | + |
| 3 | +************* |
| 4 | + Atom reader |
| 5 | +************* |
| 6 | + |
| 7 | +The next examples show some simple, more practical applications using |
| 8 | +the HTTP client. The first one reads a simple Atom feed and prints |
| 9 | +the titles of each entry to the console. |
| 10 | + |
| 11 | +The code |
| 12 | +======== |
| 13 | + |
| 14 | +.. code-block:: c++ |
| 15 | + |
| 16 | + #include "atom.hpp" |
| 17 | + #include <boost/network/protocol/http/client.hpp> |
| 18 | + #include <boost/foreach.hpp> |
| 19 | + #include <iostream> |
| 20 | + |
| 21 | + int main(int argc, char * argv[]) { |
| 22 | + using namespace boost::network; |
| 23 | + |
| 24 | + if (argc != 2) { |
| 25 | + std::cout << "Usage: " << argv[0] << " <url>" << std::endl; |
| 26 | + return 1; |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + http::client client; |
| 31 | + http::client::request request(argv[1]); |
| 32 | + request << header("Connection", "close"); |
| 33 | + http::client::response response = client.get(request); |
| 34 | + atom::feed feed(response); |
| 35 | + |
| 36 | + std::cout << "Feed: " << feed.title() |
| 37 | + << " (" << feed.subtitle() << ")" << std::endl; |
| 38 | + BOOST_FOREACH(const atom::entry &entry, feed) { |
| 39 | + std::cout << entry.title() |
| 40 | + << " (" << entry.published() << ")" << std::endl; |
| 41 | + } |
| 42 | + } |
| 43 | + catch (std::exception &e) { |
| 44 | + std::cerr << e.what() << std::endl; |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | +Building and running ``atom_reader`` |
| 51 | +==================================== |
| 52 | + |
| 53 | +.. code-block:: bash |
| 54 | + |
| 55 | + $ cd ~/cpp-netlib-build |
| 56 | + $ make atom_reader |
| 57 | + |
| 58 | +And to run the example from the command line to access the feed that |
| 59 | +lists of all the commits on cpp-netlib's master repository: |
| 60 | + |
| 61 | +.. code-block:: bash |
| 62 | + |
| 63 | + $ ./example/atom_reader https://github.com/cpp-netlib/cpp-netlib/commits/master.atom |
| 64 | + |
| 65 | +Diving into the code |
| 66 | +==================== |
| 67 | + |
| 68 | +Most of this will now be familiar. The response is passed to the |
| 69 | +constructor to the ``atom::feed`` class, which parses the resultant |
| 70 | +XML. To keep this example as simple as possible, `rapidxml`_, a |
| 71 | +header-only XML parser library, was used to parse the response. |
| 72 | + |
| 73 | +.. _`rapidxml`: http://rapidxml.sourceforge.net/ |
| 74 | + |
| 75 | +A similar example using RSS feeds exists in |
| 76 | +``libs/network/example/rss``. |
0 commit comments