Skip to content

Commit c106b79

Browse files
committed
Updating HTTP Client example.
1 parent 7abfd9b commit c106b79

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

libs/network/doc/examples.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ common networking protocols. The following set of examples describe a
77
series of realistic examples that use the :mod:`cpp-netlib` for these
88
kinds of application.
99

10-
.. todo::
11-
12-
Make the examples more accessible.
13-
1410
.. toctree::
1511
:maxdepth: 2
1612

libs/network/doc/http_client.rst

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
HTTP client
55
*************
66

7-
.. todo::
8-
9-
Rewrite text, provide a CLI example.
10-
117
The first code example is the simplest thing you can do with the
128
:mod:`cpp-netlib`. The application is a simple HTTP client, which can
139
be found in the subdirectory ``libs/network/example/http_client.cpp``.
1410
All we are doing is creating and sending an HTTP request to a server
15-
and printing the response body. Without further ado, the code to do
16-
this is as follows:
11+
and printing the response body.
12+
13+
The Code
14+
========
15+
16+
Without further ado, the code to do this is as follows:
1717

1818
.. code-block:: c++
1919

2020
#include <boost/network/protocol/http/client.hpp>
2121
#include <iostream>
2222

23-
int
24-
main(int argc, char *argv[]) {
23+
int main(int argc, char *argv[]) {
2524
using namespace boost::network;
2625
2726
if (argc != 2) {
@@ -38,6 +37,29 @@ this is as follows:
3837
return 0;
3938
}
4039

40+
Building and Running The Code
41+
=============================
42+
43+
To be build this example, you can follow the steps below without having to build
44+
the whole :mod:`cpp-netlib` distribution::
45+
46+
$ cd ~/cpp-netlib
47+
$ g++ -o http_client1 libs/network/example/http_client1.cpp \
48+
> -I. \
49+
> -I$BOOST_ROOT \
50+
> -L$BOOST_ROOT/stage/lib \
51+
> -lboost_system \
52+
> -pthread
53+
54+
You can then run this to get the Boost_ website::
55+
56+
$ ./http_client1 http://www.boost.org/
57+
58+
.. _Boost: http://www.boost.org/
59+
60+
Diving into the Code
61+
====================
62+
4163
Since this is the first example, each line will be presented and
4264
explained in detail.
4365

@@ -51,7 +73,7 @@ All the code needed for the HTTP client resides in this header.
5173

5274
http::client client;
5375

54-
First we create a ``client`` object. The ``client`` contains all the
76+
First we create a ``client`` object. The ``client`` abstracts all the
5577
connection and protocol logic. The default HTTP client is version
5678
1.1, as specified in `RFC 2616`_.
5779

libs/network/example/http_client1.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <boost/network/protocol/http/client.hpp>
2+
#include <iostream>
3+
4+
int main(int argc, char * argv[]) {
5+
using namespace boost::network;
6+
7+
if (argc != 2) { std::cout << "Usage: " << argv[0] << " <url>" << std::endl; return 1; }
8+
9+
http::client client;
10+
http::client::request request(argv[1]);
11+
request << header("Connection", "close");
12+
http::client::response response = client.get(request);
13+
std::cout << body(response) << std::endl;
14+
15+
return 0;
16+
}
17+

0 commit comments

Comments
 (0)