ITKeyword,专注技术干货聚合推荐

注册 | 登录

libevent evhttp学习——http客户端

相关推荐:为 MySQL 增加 HTTP/REST 客户端:MySQL UDF 函数 mysql-udf-http 1.0 发布

Mysql-udf-http 是一款简单的MySQL用户自定义函数(UDF, User-Defined Functions),具有http_get()、http_post()、http_put()、http_delete()四个函数,可以在M

基本环境使用版本为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请求流程http客户端使用到的接口函数及请求流程如下初始化event_base和evdns_basestruct event_base *event_base_new(void);struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);创建evhttp_request对象,并设置回调函数,这里的回调函数是和数据接收相关的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 *));创建evhttp_connection对象,并设置回调函数,这里的回调函数是和连接状态相关的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 *);有选择的向evhttp_request添加包头字段int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);发送请求int evhttp_make_request(struct evhttp_connection *evcon,

struct evhttp_request *req,

enum evhttp_cmd_type type, const char *uri);派发事件int event_base_dispatch(struct event_base *);完整代码#include "event2/http.h"#include "event2/http_struct.h"#include "event2/event.h"#include "event2/buffer.h"#include "event2/dns.h"#include "event2/thread.h"#include <stdio.h>#include <string.h>#include <assert.h>#include <sys/queue.h>#include <event.h>void RemoteReadCallback(struct evhttp_request* remote_rsp, void* arg){

event_base_loopexit((struct event_base*)arg, NULL);} int ReadHeaderDoneCallback(struct evhttp_request* remote_rsp, void* arg){

fprintf(stderr, "< HTTP/1.1 %d %s\n", evhttp_request_get_response_code(remote_rsp), evhttp_request_get_response_code_line(remote_rsp));

struct evkeyvalq* headers = evhttp_request_get_input_headers(remote_rsp);

struct evkeyval* header;

TAILQ_FOREACH(header, headers, next)

{

fprintf(stderr, "< %s: %s\n", header->key, header->value);

}

fprintf(stderr, "< \n");

return 0;}void ReadChunkCallback(struct evhttp_request* remote_rsp, void* arg){

char buf[4096];

struct evbuffer* evbuf = evhttp_request_get_input_buffer(remote_rsp);

int n = 0;

while ((n = evbuffer_remove(evbuf, buf, 4096)) > 0)

{

fwrite(buf, n, 1, stdout);

}}void RemoteRequestErrorCallback(enum evhttp_request_error error, void* arg){

fprintf(stderr, "request failed\n");

event_base_loopex

相关推荐:Android客户端转换php服务端获取的时间戳的转换

今天在用JSON获取后台的数据的时候,发现一个奇怪的现象就是返回来的时间戳都是1970年这样的,很是纠结,最后发现时php和Java中时间的格式不一样造成的,所以我

it((struct event_base*)arg, NULL);}void RemoteConnectionCloseCallback(struct evhttp_connection* connection, void* arg){

fprintf(stderr, "remote connection closed\n");

event_base_loopexit((struct event_base*)arg, NULL);}int main(int argc, char** argv){

if (argc != 2)

{

printf("usage:%s url", argv[1]);

return 1;

}

char* url = argv[1];

struct evhttp_uri* uri = evhttp_uri_parse(url);

if (!uri)

{

fprintf(stderr, "parse url failed!\n");

return 1;

}

struct event_base* base = event_base_new();

if (!base)

{

fprintf(stderr, "create event base failed!\n");

return 1;

}

struct evdns_base* dnsbase = evdns_base_new(base, 1);

if (!dnsbase)

{

fprintf(stderr, "create dns base failed!\n");

}

assert(dnsbase);

struct evhttp_request* request = evhttp_request_new(RemoteReadCallback, base);

evhttp_request_set_header_cb(request, ReadHeaderDoneCallback);

evhttp_request_set_chunked_cb(request, ReadChunkCallback);

evhttp_request_set_error_cb(request, RemoteRequestErrorCallback);

const char* host = evhttp_uri_get_host(uri);

if (!host)

{

fprintf(stderr, "parse host failed!\n");

return 1;

}

int port = evhttp_uri_get_port(uri);

if (port < 0) port = 80;

const char* request_url = url;

const char* path = evhttp_uri_get_path(uri);

if (path == NULL || strlen(path) == 0)

{

request_url = "/";

}

printf("url:%s host:%s port:%d path:%s request_url:%s\n", url, host, port, path, request_url);

struct evhttp_connection* connection =

evhttp_connection_base_new(base, dnsbase, host, port);

if (!connection)

{

fprintf(stderr, "create evhttp connection failed!\n");

return 1;

}

evhttp_connection_set_closecb(connection, RemoteConnectionCloseCallback, base);

evhttp_add_header(evhttp_request_get_output_headers(request), "Host", host);

evhttp_make_request(connection, request, EVHTTP_REQ_GET, request_url);

event_base_dispatch(base);

return 0;}编译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运行示例,这里只打印了包头字段 $ ./http_client http://www.qq.com >/dev/null< HTTP/1.1 200 OK< Server: squid/3.4.3< Content-Type: text/html; charset=GB2312< Cache-Control: max-age=60< Expires: Fri, 05 Aug 2016 08:48:31 GMT< Date: Fri, 05 Aug 2016 08:47:31 GMT< Transfer-Encoding: chunked< Connection: keep-alive< Connection: Transfer-Encoding<

$(function () {

$('pre.prettyprint code').each(function () {

var lines = $(this).text().split('\n').length;

var $numbering = $('<ul/>').addClass('pre-numbering').hide();

$(this).addClass('has-numbering').parent().append($numbering);

for (i = 1; i <= lines; i++) {

$numbering.append($('<li/>').text(i));

};

$numbering.fadeIn(1700);

});

});

相关推荐:libcurl库进行http通讯-基于 C 的 HTTP 客户端

之前两篇博客介绍了一些最简单的基本知识,今天就来应用一下,写一个HTTP客户端。 以下有些内容,参考出处: http://www.oschina.net/question/54100_8602首

基本环境使用版本为libevent-2.1.5,目前为beta版,其中evhttp和旧版区别在于新增了如下接口// 设置回调函数,在包头读取完成后回调void evhttp_request_set_header_cb (struct evhttp_r...

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。