c - 具有请求超时的 libevent http 客户端

标签 c http libevent

我正在使用 libevent 在特定时间间隔内获取网站的一些统计信息。我的程序基于 this .我唯一缺少的是请求超时,最好是亚秒级精度。

我已经尝试了一些东西,但无法让它工作。我真的很感激任何关于此的指示。

最佳答案

这是一个非常粗略的例子:我使用 libevent 计时器来实现超时。这可以通过将计时器放在结构中并在回调中取消它来进一步改进。

#include <stdio.h>
#include <event.h>
#include <evhttp.h>
#include <stdlib.h>
#include <unistd.h>


struct http_client {
    struct evhttp_connection *conn;
    struct evhttp_request *req;
    bool finished;
};

void _reqhandler(struct evhttp_request *req, void *state) {
    struct http_client *hc = (http_client*)state;
    hc->finished = true;

    if (req == NULL) {
        printf("timed out!\n");
    } else if (req->response_code == 0) {
        printf("connection refused!\n");
    } else if (req->response_code != 200) {
        printf("error: %u %s\n", req->response_code, req->response_code_line);
    } else {
        printf("success: %u %s\n", req->response_code, req->response_code_line);
    }
    event_loopexit(NULL);
}

void timeout_cb(int fd, short event, void *arg) {
    struct http_client *hc = (http_client*)arg;
    printf("Timed out\n");

    if (hc->finished == false){ // Can't cancel request if the callback has already executed
        evhttp_cancel_request(hc->req);
    }
}

int main(int argc, char *argv[]) {
    struct http_client *hc = (struct http_client *)malloc(sizeof(struct http_client));
    hc->finished = false;

    struct event ev;
    struct timeval tv;

    tv.tv_sec = 3; // Timeout is set to 3.005 seconds
    tv.tv_usec = 5000;


    const char *addr = "173.194.39.64"; //google.com
    unsigned int port = 80;

    event_init();

    hc->conn = evhttp_connection_new(addr, port);
    evhttp_connection_set_timeout(hc->conn, 5);

    hc->req = evhttp_request_new(_reqhandler, (void*)hc);
    evhttp_add_header(hc->req->output_headers, "Host", addr);
    evhttp_add_header(hc->req->output_headers, "Content-Length", "0");
    evhttp_make_request(hc->conn, hc->req, EVHTTP_REQ_GET, "/");

    evtimer_set(&ev, timeout_cb, (void*)hc); // Set a timer to cancel the request after certain time
    evtimer_add(&ev, &tv);

    printf("starting event loop..\n");
    printf("\n");
    event_dispatch();

    return 0;
}

关于c - 具有请求超时的 libevent http 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002211/

相关文章:

c - Mcrt1.o和Scrt1.o有什么用?

c - 使用 pragma 抑制 gcc 4.2.1 警告

将希腊单词转换为大写

html - 关于 http-server 与在 NodeJS 中使用 http 的混淆

java - 如何使用 xpath 从特定站点获取值并将其存储在数组中? (安卓)

golang cgo : libevent handler values are set to null during execution

c - nm 符号实用程序报告

javascript - 如何将数据主体发送到 XMLHttpRequest,看起来像这样?

c - libevent,之后启动 SSL

c - Libevent:如何暂时禁用,然后重新启用 EV_READ? (在 C 中)