-
Notifications
You must be signed in to change notification settings - Fork 0
/
libevent evhttp学习——http客户端.html
351 lines (320 loc) · 32.2 KB
/
libevent evhttp学习——http客户端.html
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="littlewhite" />
<meta name="copyright" content="littlewhite" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="c++, libevent, http, Language, " />
<meta property="og:title" content="libevent evhttp学习——http客户端 "/>
<meta property="og:url" content="https://chukeer.github.io/libevent evhttp学习——http客户端.html" />
<meta property="og:description" content="基本环境¶ 使用版本为libevent-2.1.5,目前为beta版,其中evhttp和旧版区别在于新增了如下接口 // 设置回调函数,在包头读取完成后回调 void evhttp_request_set_header_cb (struct evhttp_request *, int(*cb)(struct evhttp_request *, void *)) // 设置回调函数,在body有数据返回后回调 void evhttp_request_set_chunked_cb (struct evhttp_request *, void(*cb)(struct evhttp_request *, void *)) 这样的好处是可以在合适的时机回调我们注册的回调函数,比如下载1G的文件,在之前的版本只有下载完成后才会回调,现在每下载一部分数据就会回调一次,让上层应用更加灵活,尤其在http代理时,可以做到边下载边回复 2.1.5版本的完整接口文档详见http://www.wangafu.net/~nickm/libevent-2.1/doxygen/html/http_8h.html 请求流程 ..." />
<meta property="og:site_name" content="楚客" />
<meta property="og:article:author" content="littlewhite" />
<meta property="og:article:published_time" content="2016-08-05T00:00:00+08:00" />
<meta property="" content="2016-08-05T00:00:00+08:00" />
<meta name="twitter:title" content="libevent evhttp学习——http客户端 ">
<meta name="twitter:description" content="基本环境¶ 使用版本为libevent-2.1.5,目前为beta版,其中evhttp和旧版区别在于新增了如下接口 // 设置回调函数,在包头读取完成后回调 void evhttp_request_set_header_cb (struct evhttp_request *, int(*cb)(struct evhttp_request *, void *)) // 设置回调函数,在body有数据返回后回调 void evhttp_request_set_chunked_cb (struct evhttp_request *, void(*cb)(struct evhttp_request *, void *)) 这样的好处是可以在合适的时机回调我们注册的回调函数,比如下载1G的文件,在之前的版本只有下载完成后才会回调,现在每下载一部分数据就会回调一次,让上层应用更加灵活,尤其在http代理时,可以做到边下载边回复 2.1.5版本的完整接口文档详见http://www.wangafu.net/~nickm/libevent-2.1/doxygen/html/http_8h.html 请求流程 ...">
<title>libevent evhttp学习——http客户端 · 楚客
</title>
<!--
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
--!>
<link href="https://chukeer.github.io/theme/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://chukeer.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/pygments.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/tipuesearch/tipuesearch.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/elegant.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/custom.css" media="screen">
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="https://chukeer.github.io/"><span class=site-name>楚客</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="https://chukeer.github.io">Home</a></li>
<li ><a href="https://chukeer.github.io/categories.html">Categories</a></li>
<li ><a href="https://chukeer.github.io/tags.html">Tags</a></li>
<li ><a href="https://chukeer.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="https://chukeer.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="https://chukeer.github.io/libevent evhttp学习——http客户端.html"> libevent evhttp学习——http客户端 </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span2 table-of-content">
<nav>
<h4>Contents</h4>
<div class="toc">
<ul>
<li><a href="#_1">基本环境</a></li>
<li><a href="#_2">请求流程</a></li>
<li><a href="#_3">完整代码</a></li>
</ul>
</div>
</nav>
</div>
<div class="span8 article-content">
<h2 id="_1">基本环境<a class="headerlink" href="#_1" title="Permanent link">¶</a></h2>
<p>使用版本为libevent-2.1.5,目前为beta版,其中evhttp和旧版区别在于新增了如下接口</p>
<div class="highlight"><pre><span></span><span class="c1">// 设置回调函数,在包头读取完成后回调</span>
<span class="kt">void</span> <span class="n">evhttp_request_set_header_cb</span> <span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_request</span> <span class="o">*</span><span class="p">,</span> <span class="kt">int</span><span class="p">(</span><span class="o">*</span><span class="n">cb</span><span class="p">)(</span><span class="k">struct</span> <span class="n">evhttp_request</span> <span class="o">*</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="p">))</span>
<span class="c1">// 设置回调函数,在body有数据返回后回调</span>
<span class="kt">void</span> <span class="n">evhttp_request_set_chunked_cb</span> <span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_request</span> <span class="o">*</span><span class="p">,</span> <span class="kt">void</span><span class="p">(</span><span class="o">*</span><span class="n">cb</span><span class="p">)(</span><span class="k">struct</span> <span class="n">evhttp_request</span> <span class="o">*</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="p">))</span>
</pre></div>
<p>这样的好处是可以在合适的时机回调我们注册的回调函数,比如下载1G的文件,在之前的版本只有下载完成后才会回调,现在每下载一部分数据就会回调一次,让上层应用更加灵活,尤其在http代理时,可以做到边下载边回复</p>
<p>2.1.5版本的完整接口文档详见<a href="http://www.wangafu.net/~nickm/libevent-2.1/doxygen/html/http_8h.html">http://www.wangafu.net/~nickm/libevent-2.1/doxygen/html/http_8h.html</a></p>
<h2 id="_2">请求流程<a class="headerlink" href="#_2" title="Permanent link">¶</a></h2>
<p>http客户端使用到的接口函数及请求流程如下</p>
<ol>
<li>初始化event_base和evdns_base</li>
</ol>
<div class="highlight"><pre><span></span> <span class="k">struct</span> <span class="n">event_base</span> <span class="o">*</span><span class="nf">event_base_new</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
<span class="k">struct</span> <span class="n">evdns_base</span> <span class="o">*</span> <span class="nf">evdns_base_new</span><span class="p">(</span><span class="k">struct</span> <span class="n">event_base</span> <span class="o">*</span><span class="n">event_base</span><span class="p">,</span> <span class="kt">int</span> <span class="n">initialize_nameservers</span><span class="p">);</span>
</pre></div>
<ol>
<li>
<p>创建evhttp_request对象,并设置回调函数,这里的回调函数是和数据接收相关的</p>
<p><code>cpp
struct evhttp_request *evhttp_request_new(void (*cb)(struct evhttp_request *, void *), void *arg);
void evhttp_request_set_header_cb(struct evhttp_request *, int (*cb)(struct evhttp_request *, void *));
void evhttp_request_set_chunked_cb(struct evhttp_request *, void (*cb)(struct evhttp_request *, void *));
void evhttp_request_set_error_cb(struct evhttp_request *, void (*)(enum evhttp_request_error, void *));</code>
3. 创建evhttp_connection对象,并设置回调函数,这里的回调函数是和连接状态相关的</p>
<p><code>cpp
struct evhttp_connection *evhttp_connection_base_new(struct event_base *base,
struct evdns_base *dnsbase, const char *address, unsigned short port);
void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
void (*)(struct evhttp_connection *, void *), void *);</code>
4. 有选择的向evhttp_request添加包头字段</p>
<p><code>cpp
int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);</code>
5. 发送请求</p>
<p><code>cpp
int evhttp_make_request(struct evhttp_connection *evcon,
struct evhttp_request *req,
enum evhttp_cmd_type type, const char *uri);</code>
6. 派发事件</p>
<p><code>cpp
int event_base_dispatch(struct event_base *);</code></p>
</li>
</ol>
<h2 id="_3">完整代码<a class="headerlink" href="#_3" title="Permanent link">¶</a></h2>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">"event2/http.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"event2/http_struct.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"event2/event.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"event2/buffer.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"event2/dns.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"event2/thread.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><stdio.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><string.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><assert.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><sys/queue.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><event.h></span><span class="cp"></span>
<span class="kt">void</span> <span class="nf">RemoteReadCallback</span><span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_request</span><span class="o">*</span> <span class="n">remote_rsp</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">event_base_loopexit</span><span class="p">((</span><span class="k">struct</span> <span class="n">event_base</span><span class="o">*</span><span class="p">)</span><span class="n">arg</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">ReadHeaderDoneCallback</span><span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_request</span><span class="o">*</span> <span class="n">remote_rsp</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"< HTTP/1.1 %d %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">evhttp_request_get_response_code</span><span class="p">(</span><span class="n">remote_rsp</span><span class="p">),</span> <span class="n">evhttp_request_get_response_code_line</span><span class="p">(</span><span class="n">remote_rsp</span><span class="p">));</span>
<span class="k">struct</span> <span class="n">evkeyvalq</span><span class="o">*</span> <span class="n">headers</span> <span class="o">=</span> <span class="n">evhttp_request_get_input_headers</span><span class="p">(</span><span class="n">remote_rsp</span><span class="p">);</span>
<span class="k">struct</span> <span class="n">evkeyval</span><span class="o">*</span> <span class="n">header</span><span class="p">;</span>
<span class="n">TAILQ_FOREACH</span><span class="p">(</span><span class="n">header</span><span class="p">,</span> <span class="n">headers</span><span class="p">,</span> <span class="n">next</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"< %s: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">header</span><span class="o">-></span><span class="n">key</span><span class="p">,</span> <span class="n">header</span><span class="o">-></span><span class="n">value</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"< </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">ReadChunkCallback</span><span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_request</span><span class="o">*</span> <span class="n">remote_rsp</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">4096</span><span class="p">];</span>
<span class="k">struct</span> <span class="n">evbuffer</span><span class="o">*</span> <span class="n">evbuf</span> <span class="o">=</span> <span class="n">evhttp_request_get_input_buffer</span><span class="p">(</span><span class="n">remote_rsp</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">while</span> <span class="p">((</span><span class="n">n</span> <span class="o">=</span> <span class="n">evbuffer_remove</span><span class="p">(</span><span class="n">evbuf</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="mi">4096</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fwrite</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stdout</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">RemoteRequestErrorCallback</span><span class="p">(</span><span class="k">enum</span> <span class="n">evhttp_request_error</span> <span class="n">error</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"request failed</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">event_base_loopexit</span><span class="p">((</span><span class="k">struct</span> <span class="n">event_base</span><span class="o">*</span><span class="p">)</span><span class="n">arg</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">RemoteConnectionCloseCallback</span><span class="p">(</span><span class="k">struct</span> <span class="n">evhttp_connection</span><span class="o">*</span> <span class="n">connection</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"remote connection closed</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">event_base_loopexit</span><span class="p">((</span><span class="k">struct</span> <span class="n">event_base</span><span class="o">*</span><span class="p">)</span><span class="n">arg</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">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">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">printf</span><span class="p">(</span><span class="s">"usage:%s url"</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="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">char</span><span class="o">*</span> <span class="n">url</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
<span class="k">struct</span> <span class="n">evhttp_uri</span><span class="o">*</span> <span class="n">uri</span> <span class="o">=</span> <span class="n">evhttp_uri_parse</span><span class="p">(</span><span class="n">url</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">uri</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"parse url failed!</span><span class="se">\n</span><span class="s">"</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="k">struct</span> <span class="n">event_base</span><span class="o">*</span> <span class="n">base</span> <span class="o">=</span> <span class="n">event_base_new</span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">base</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"create event base failed!</span><span class="se">\n</span><span class="s">"</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="k">struct</span> <span class="n">evdns_base</span><span class="o">*</span> <span class="n">dnsbase</span> <span class="o">=</span> <span class="n">evdns_base_new</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">dnsbase</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"create dns base failed!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">assert</span><span class="p">(</span><span class="n">dnsbase</span><span class="p">);</span>
<span class="k">struct</span> <span class="n">evhttp_request</span><span class="o">*</span> <span class="n">request</span> <span class="o">=</span> <span class="n">evhttp_request_new</span><span class="p">(</span><span class="n">RemoteReadCallback</span><span class="p">,</span> <span class="n">base</span><span class="p">);</span>
<span class="n">evhttp_request_set_header_cb</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">ReadHeaderDoneCallback</span><span class="p">);</span>
<span class="n">evhttp_request_set_chunked_cb</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">ReadChunkCallback</span><span class="p">);</span>
<span class="n">evhttp_request_set_error_cb</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">RemoteRequestErrorCallback</span><span class="p">);</span>
<span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">host</span> <span class="o">=</span> <span class="n">evhttp_uri_get_host</span><span class="p">(</span><span class="n">uri</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">host</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"parse host failed!</span><span class="se">\n</span><span class="s">"</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="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="n">evhttp_uri_get_port</span><span class="p">(</span><span class="n">uri</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">port</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="n">port</span> <span class="o">=</span> <span class="mi">80</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">request_url</span> <span class="o">=</span> <span class="n">url</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">path</span> <span class="o">=</span> <span class="n">evhttp_uri_get_path</span><span class="p">(</span><span class="n">uri</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">path</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">strlen</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">request_url</span> <span class="o">=</span> <span class="s">"/"</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"url:%s host:%s port:%d path:%s request_url:%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">request_url</span><span class="p">);</span>
<span class="k">struct</span> <span class="n">evhttp_connection</span><span class="o">*</span> <span class="n">connection</span> <span class="o">=</span> <span class="n">evhttp_connection_base_new</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">dnsbase</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">connection</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"create evhttp connection failed!</span><span class="se">\n</span><span class="s">"</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">evhttp_connection_set_closecb</span><span class="p">(</span><span class="n">connection</span><span class="p">,</span> <span class="n">RemoteConnectionCloseCallback</span><span class="p">,</span> <span class="n">base</span><span class="p">);</span>
<span class="n">evhttp_add_header</span><span class="p">(</span><span class="n">evhttp_request_get_output_headers</span><span class="p">(</span><span class="n">request</span><span class="p">),</span> <span class="s">"Host"</span><span class="p">,</span> <span class="n">host</span><span class="p">);</span>
<span class="n">evhttp_make_request</span><span class="p">(</span><span class="n">connection</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">EVHTTP_REQ_GET</span><span class="p">,</span> <span class="n">request_url</span><span class="p">);</span>
<span class="n">event_base_dispatch</span><span class="p">(</span><span class="n">base</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>
<p>编译</p>
<div class="highlight"><pre><span></span>g++ http_client.cpp -I/opt/local/libevent-2.1.5/include -L/opt/local/libevent-2.1.5/lib -levent -g -o http_client
</pre></div>
<p>运行示例,这里只打印了包头字段</p>
<div class="highlight"><pre><span></span> <span class="o">$</span> <span class="o">./</span><span class="nt">http_client</span> <span class="nt">http</span><span class="o">://</span><span class="nt">www</span><span class="nc">.qq.com</span> <span class="o">>/</span><span class="nt">dev</span><span class="o">/</span><span class="nt">null</span>
<span class="o"><</span> <span class="nt">HTTP</span><span class="o">/</span><span class="nt">1</span><span class="nc">.1</span> <span class="nt">200</span> <span class="nt">OK</span>
<span class="o"><</span> <span class="nt">Server</span><span class="o">:</span> <span class="nt">squid</span><span class="o">/</span><span class="nt">3</span><span class="nc">.4.3</span>
<span class="o"><</span> <span class="nt">Content-Type</span><span class="o">:</span> <span class="nt">text</span><span class="o">/</span><span class="nt">html</span><span class="o">;</span> <span class="nt">charset</span><span class="o">=</span><span class="nt">GB2312</span>
<span class="o"><</span> <span class="nt">Cache-Control</span><span class="o">:</span> <span class="nt">max-age</span><span class="o">=</span><span class="nt">60</span>
<span class="o"><</span> <span class="nt">Expires</span><span class="o">:</span> <span class="nt">Fri</span><span class="o">,</span> <span class="nt">05</span> <span class="nt">Aug</span> <span class="nt">2016</span> <span class="nt">08</span><span class="nd">:48:31</span> <span class="nt">GMT</span>
<span class="o"><</span> <span class="nt">Date</span><span class="o">:</span> <span class="nt">Fri</span><span class="o">,</span> <span class="nt">05</span> <span class="nt">Aug</span> <span class="nt">2016</span> <span class="nt">08</span><span class="nd">:47:31</span> <span class="nt">GMT</span>
<span class="o"><</span> <span class="nt">Transfer-Encoding</span><span class="o">:</span> <span class="nt">chunked</span>
<span class="o"><</span> <span class="nt">Connection</span><span class="o">:</span> <span class="nt">keep-alive</span>
<span class="o"><</span> <span class="nt">Connection</span><span class="o">:</span> <span class="nt">Transfer-Encoding</span>
<span class="o"><</span>
</pre></div>
<hr/>
<aside>
<nav>
<ul class="articles-timeline">
<li class="previous-article">« <a href="https://chukeer.github.io/C++ and Python 多线程笔记.html" title="Previous: C++ & Python 多线程笔记">C++ & Python 多线程笔记</a></li>
<li class="next-article"><a href="https://chukeer.github.io/libevent evhttp学习——http服务端.html" title="Next: libevent evhttp学习——http服务端">libevent evhttp学习——http服务端</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<table class="table">
<!-- <time pubdate="pubdate" datetime="2016-08-05T00:00:00+08:00"> 8 5, 2016</time> -->
<tr>
<td>Published</td>
<td><time pubdate="pubdate" datetime="2016-08-05T00:00:00+08:00">2016- 8-5</time></td>
</tr>
<tr>
<td>Category</td>
<td><a class="category-link" href="https://chukeer.github.io/categories.html#language-ref">Language</a></td>
</tr>
<tr>
<td>Tags</td>
<td>
<ul class="list-of-tags tags-in-article">
<li><a href="https://chukeer.github.io/tags.html#c-ref">c++
<span>15</span>
</a></li>
<li><a href="https://chukeer.github.io/tags.html#http-ref">http
<span>3</span>
</a></li>
<li><a href="https://chukeer.github.io/tags.html#libevent-ref">libevent
<span>2</span>
</a></li>
</ul>
</td>
</tr>
</table>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
<div id="push"></div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <!--
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
-->
<script src="https://chukeer.github.io/theme/js/jquery.min.js"></script>
<script src="https://chukeer.github.io/theme/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
$("div.article-content table").addClass("table table-hover");
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : http://oncrashreboot.com/pelican-elegant -->
</html>