Menu

[r224]: / trunk / libs / network / doc / quick_start.cpp  Maximize  Restore  History

Download this file

55 lines (45 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Glyn Matthews 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//[ quick_start_main
/*`
Here is an example of __cnl__ at it's absolute simplest. It
downloads a file over HTTP from a URL given as a command line
argument and prints the response to the console.
*/
#include <boost/network.hpp>
#include <iostream>
int
main(int argc, char *argv[]) {
namespace http = boost::network::http;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " url" << std::endl;
return 1;
}
try {
/*<< Create the client >>*/
http::client client;
/*<< Create a request using a URI supplied on the command
line >>*/
http::request request(argv[1]);
/*<< Get a response >>*/
http::response response = client.get(request);
/*<< Iterate through the response headers and print them to
the console >>*/
headers_range<http::response>::type hdrs = headers(response);
boost::range_iterator<headers_range<http::response>::type>::type
it = boost::begin(hdrs), end = boost::end(hdrs);
for (; it != end; ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
/*<< Print the response body to the console >>*/
std::cout << http::body(response) << std::endl;
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
//]