-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClient.cxx
More file actions
73 lines (59 loc) · 1.82 KB
/
httpClient.cxx
File metadata and controls
73 lines (59 loc) · 1.82 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <boost/network/protocol/http/client.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/program_options.hpp>
#include <string>
#include <sstream>
#include <iostream>
//#include <memory>
#include <boost/shared_ptr.hpp>
namespace http = boost::network::http;
namespace asio = boost::asio;
namespace po = boost::program_options;
int main(int argc, char * argv[]) {
std::string host;
std::string port;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "display this message")
("host,s", po::value<std::string>(&host)->default_value("localhost"), "host to connect to.")
("port,p", po::value<std::string>(&port)->default_value("80"), "port to listen on")
;
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if(vm.count("help")) {
desc.print(std::cout);
exit(0);
}
po::notify(vm);
} catch (std::exception& ex) {
std::cerr << "Missing required option: \n";
std::cerr << ex.what() << "\n";
std::cerr << "Useage:\n";
desc.print(std::cerr);
exit(1);
}
boost::shared_ptr<asio::io_service> io_service { boost::make_shared<asio::io_service>() };
http::client client {
http::client::options {}
.io_service(io_service) };
try {
std::ostringstream url;
url << "http://" << host << ":" << port << "/";
for (int i=0; i<5; i++) {
http::client::request request(url.str());
request << boost::network::header("Connection", "close");
http::client::response response =
client.get(request);
std::cout << body(response) << std::endl;
}
io_service->stop();
} catch (std::exception & e) {
std::cerr << e.what() << std::endl;
io_service->stop();
return 1;
}
return 0;
}
/* vim:set tabstop=4 shiftwidth=4 fo=cqwan autoindent : */
/* makeprg=make\ -C\ ~/tmp/build-try-https */