用libevent evhttp流客户端请求体?

时间:2017-03-03 09:55:01

标签: c http libevent

我想使用libevent和evhttp来传输客户端POST请求主体。我已经找到了使用固定主体发送请求的示例,但我不确定如何设置一个带有正文的请求,我将需要在不确定的时间段内连续写入和更新。有可能这样做会解放吗?我目前的代码基线看起来像这样:

#include <evhttp.h>
#include <event2/event.h>
#include <event2/http.h>

void http_request_done(struct evhttp_request *req, void *arg) {
  printf("DONE!\n");
}

int main(int argc, char **argv) {
  struct event_base *base = event_base_new();
  struct evhttp_connection *conn = evhttp_connection_base_new(base, NULL, "127.0.0.1", 3000);
  struct evhttp_request *req = evhttp_request_new(http_request_done, NULL);

  evhttp_make_request(conn, req, EVHTTP_REQ_POST, "/");
  evhttp_connection_set_timeout(req->evcon, 600);
  event_base_loop(base, EVLOOP_NONBLOCK);
  event_base_dispatch(base);

  return 0;
}

如何使用流媒体发送POST请求?

1 个答案:

答案 0 :(得分:0)

LibEvent为此设置了功能块。您可以看到代码示例like thisthis one

我们可以在start()/ end()之间的循环中看到in documentation那些函数 - chunk():`

  

evhttp_send_reply_start (struct evhttp_request * req)

     

evhttp_send_reply_chunk (struct evhttp_request * req,struct evbuffer * databuf)

     

evhttp_send_reply_end (struct evhttp_request * req)`。

这些用于发送,如果您需要获取传入的分块数据,则evhttp_request_set_chunked_cb()

相关问题