Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc106b79

Browse files
committed
Updating HTTP Client example.
1 parent7abfd9b commitc106b79

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 theBoost_ 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+
intmain(int argc,char * argv[]) {
5+
usingnamespaceboost::network;
6+
7+
if (argc !=2) { std::cout <<"Usage:" << argv[0] <<" <url>" << std::endl;return1; }
8+
9+
http::client client;
10+
http::client::requestrequest(argv[1]);
11+
request <<header("Connection","close");
12+
http::client::response response = client.get(request);
13+
std::cout <<body(response) << std::endl;
14+
15+
return0;
16+
}
17+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp