-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttp_client.html
More file actions
244 lines (212 loc) · 14.1 KB
/
http_client.html
File metadata and controls
244 lines (212 loc) · 14.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTTP client — cpp-netlib v0.9 documentation</title>
<link rel="stylesheet" href="_static/cpp-netlib.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.9',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="cpp-netlib v0.9 documentation" href="index.html" />
<link rel="up" title="HTTP examples" href="examples_http.html" />
<link rel="next" title="“Hello world” HTTP server" href="hello_world_server.html" />
<link rel="prev" title="HTTP examples" href="examples_http.html" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19815738-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="document">
<div id="custom-doc" class="yui-t4">
<div id="hd">
<h1><a href="index.html">cpp-netlib v0.9 documentation</a></h1>
<div class="nav">
« <a href="examples_http.html" title="HTTP examples">previous</a>
|
<a href="examples.html" title="Examples" accesskey="U">up</a>
|
<a href="hello_world_server.html" title="&#8220;Hello world&#8221; HTTP server">next</a> »</div>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<div class="yui-g" id="http_client">
<div class="section" id="http-client">
<span id="id1"></span><h1>HTTP client<a class="headerlink" href="#http-client" title="Permalink to this headline">¶</a></h1>
<p>The first code example is the simplest thing you can do with the
<tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt>. The application is a simple HTTP client, which can
be found in the subdirectory <tt class="docutils literal"><span class="pre">libs/network/example/http_client.cpp</span></tt>.
All we are doing is creating and sending an HTTP request to a server
and printing the response body.</p>
<div class="section" id="the-code">
<h2>The Code<a class="headerlink" href="#the-code" title="Permalink to this headline">¶</a></h2>
<p>Without further ado, the code to do this is as follows:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include <boost/network/protocol/http/client.hpp></span>
<span class="cp">#include <iostream></span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">network</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Usage: "</span> <span class="o"><<</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o"><<</span> <span class="s">" [url]"</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">http</span><span class="o">::</span><span class="n">client</span> <span class="n">client</span><span class="p">;</span>
<span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">request</span> <span class="n">request</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<span class="n">request</span> <span class="o"><<</span> <span class="n">header</span><span class="p">(</span><span class="s">"Connection"</span><span class="p">,</span> <span class="s">"close"</span><span class="p">);</span>
<span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">body</span><span class="p">(</span><span class="n">response</span><span class="p">)</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="building-and-running-the-code">
<h2>Building and Running The Code<a class="headerlink" href="#building-and-running-the-code" title="Permalink to this headline">¶</a></h2>
<p>To be build this example, you can follow the steps below without having to build
the whole <tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> distribution:</p>
<div class="highlight-python"><pre>$ cd ~/cpp-netlib
$ g++ -o http_client1 libs/network/example/http_client1.cpp \
> -I. \
> -I$BOOST_ROOT \
> -L$BOOST_ROOT/stage/lib \
> -lboost_system \
> -pthread</pre>
</div>
<p>You can then run this to get the <a class="reference external" href="http://www.boost.org/">Boost</a> website:</p>
<div class="highlight-python"><pre>$ ./http_client1 http://www.boost.org/</pre>
</div>
</div>
<div class="section" id="diving-into-the-code">
<h2>Diving into the Code<a class="headerlink" href="#diving-into-the-code" title="Permalink to this headline">¶</a></h2>
<p>Since this is the first example, each line will be presented and
explained in detail.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include <boost/network/protocol/http/client.hpp></span>
</pre></div>
</div>
<p>All the code needed for the HTTP client resides in this header.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">http</span><span class="o">::</span><span class="n">client</span> <span class="n">client</span><span class="p">;</span>
</pre></div>
</div>
<p>First we create a <tt class="docutils literal"><span class="pre">client</span></tt> object. The <tt class="docutils literal"><span class="pre">client</span></tt> abstracts all the
connection and protocol logic. The default HTTP client is version
1.1, as specified in <a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</a>.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">request</span> <span class="n">request</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
</pre></div>
</div>
<p>Next, we create a <tt class="docutils literal"><span class="pre">request</span></tt> object, with a URI string passed as a
constructor argument.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">request</span> <span class="o"><<</span> <span class="n">header</span><span class="p">(</span><span class="s">"Connection"</span><span class="p">,</span> <span class="s">"close"</span><span class="p">);</span>
</pre></div>
</div>
<p><tt class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></tt> makes use of stream syntax and <em>directives</em> to allow
developers to build complex message structures with greater
flexibility and clarity. Here, we add the HTTP header “Connection:
close” to the request in order to signal that the connection will be
closed after the request has completed.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="o">::</span><span class="n">response</span> <span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
</pre></div>
</div>
<p>Once we’ve built the request, we then make an HTTP GET request
throught the <tt class="docutils literal"><span class="pre">http::client</span></tt> from which an <tt class="docutils literal"><span class="pre">http::response</span></tt> is
returned. <tt class="docutils literal"><span class="pre">http::client</span></tt> supports all common HTTP methods: GET,
POST, HEAD, DELETE.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">body</span><span class="p">(</span><span class="n">response</span><span class="p">)</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</pre></div>
</div>
<p>Finally, though we don’t do any error checking, the response body is
printed to the console using the <tt class="docutils literal"><span class="pre">body</span></tt> directive.</p>
<p>That’s all there is to the HTTP client. In fact, it’s possible to
compress this to a single line:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">body</span><span class="p">(</span><span class="n">http</span><span class="o">::</span><span class="n">client</span><span class="p">().</span><span class="n">get</span><span class="p">(</span><span class="n">http</span><span class="o">::</span><span class="n">request</span><span class="p">(</span><span class="s">"http://www.boost.org/"</span><span class="p">)));</span>
</pre></div>
</div>
<p>Next we’ll develop a simple client/server application using
<tt class="docutils literal"><span class="pre">http::server</span></tt> and <tt class="docutils literal"><span class="pre">http::client</span></tt>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="yui-b" id="sidebar">
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">HTTP client</a><ul>
<li><a class="reference internal" href="#the-code">The Code</a></li>
<li><a class="reference internal" href="#building-and-running-the-code">Building and Running The Code</a></li>
<li><a class="reference internal" href="#diving-into-the-code">Diving into the Code</a></li>
</ul>
</li>
</ul>
<h3>Browse</h3>
<ul>
<li>Prev: <a href="examples_http.html">HTTP examples</a></li>
<li>Next: <a href="hello_world_server.html">“Hello world” HTTP server</a></li>
</ul>
<h3>You are here:</h3>
<ul>
<li>
<a href="index.html">cpp-netlib v0.9 documentation</a>
<ul><li><a href="examples.html">Examples</a>
<ul><li><a href="examples_http.html">HTTP examples</a>
<ul><li>HTTP client</li></ul>
</li></ul></li></ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/http_client.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
</div>
</div>
<div id="ft">
<div class="nav">
« <a href="examples_http.html" title="HTTP examples">previous</a>
|
<a href="examples.html" title="Examples" accesskey="U">up</a>
|
<a href="hello_world_server.html" title="&#8220;Hello world&#8221; HTTP server">next</a> »</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
documentation automatically generated by <a href="http://sphinx.pocoo.org">Sphinx</a> | style mostly stolen from <a href="http://lettuce.it">lettuce.it</a>
| background image attributed to <a href="http://www.flickr.com/photos/OrangeSmell">OrangeSmell</a>
</div>
</body>
</html>