Skip to content

Commit 80be9bf

Browse files
committed
A simple client that talks to the hello world server.
1 parent 7726ab7 commit 80be9bf

File tree

1 file changed

+89
-4
lines changed

1 file changed

+89
-4
lines changed

libs/network/doc/hello_world_client.rst

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,96 @@
44
"Hello world" HTTP client
55
***************************
66

7-
.. todo::
7+
Since we have a "Hello World" HTTP server, let's then create an HTTP client to
8+
access that server. This client will be similar to the HTTP client we made
9+
earlier in the documentation.
810

9-
Take the HTTP client example and server example and describe
10-
what's happening.
11+
The Code
12+
========
13+
14+
We want to create a simple HTTP client that just makes a request to the HTTP
15+
server that we created earlier. This really simple client will look like this:
16+
17+
.. code-block:: c++
18+
19+
#include <boost/network/protocol/http/client.hpp>
20+
#include <string>
21+
#include <sstream>
22+
#include <iostream>
23+
24+
namespace http = boost::network::http;
25+
26+
int main(int argc, char * argv[]) {
27+
if (argc != 3) {
28+
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
29+
return 1;
30+
}
31+
32+
try {
33+
http::client client;
34+
std::ostringstream url;
35+
url << "http://" << argv[1] << ":" << argv[2] << "/";
36+
http::client::request request(url.str());
37+
http::client::response response =
38+
client.get(request);
39+
std::cout << body(response) << std::endl;
40+
} catch (std::exception & e) {
41+
std::cerr << e.what() << std::endl;
42+
return 1;
43+
}
44+
return 0;
45+
}
46+
47+
Building the Client
48+
===================
49+
50+
Just like with the HTTP Server and HTTP client example before, we can build this
51+
example by doing the following on the shell:
52+
53+
.. code-block:: bash
54+
55+
$ cd ~/cpp-netlib
56+
$ g++ -o hello_world_client \
57+
> libs/network/example/http/hello_world_client.cpp \
58+
> -I$BOOST_ROOT \
59+
> -I. \
60+
> -L$BOOST_ROOT/stage/lib \
61+
> -lboost_system \
62+
> -pthread
63+
64+
This example can be run from the command line as follows:
1165

1266
::
1367

14-
./hello_world_client http://127.0.0.1/
68+
$ ./hello_world_client 127.0.0.1 8000
69+
70+
.. note:: This assumes that you have the ``hello_world_server`` running on
71+
localhost port 8000.
72+
73+
Diving into the Code
74+
====================
75+
76+
All this example shows is how easy it is to write an HTTP client that connects
77+
to an HTTP server, and gets the body of the response. The relevant lines are:
78+
79+
.. code-block:: c++
80+
81+
http::client client;
82+
http::client::request request(url.str());
83+
http::client::response response =
84+
client.get(request);
85+
std::cout << body(response) << std::endl;
86+
87+
You can then imagine using this in an XML-RPC client, where you can craft the
88+
XML-RPC request as payload which you can pass as the body to a request, then
89+
perform the request via HTTP:
90+
91+
.. code-block:: c++
92+
93+
http::client client;
94+
http::client::request request("http://my.webservice.com/");
95+
http::client::response =
96+
client.post(request, "application/xml", some_xml_string);
97+
std::data = body(response);
98+
99+

0 commit comments

Comments
 (0)