【问题标题】:Stream client request body with libevent evhttp?使用 libevent evhttp 流式传输客户端请求正文?
【发布时间】:2017-03-03 09:55:00
【问题描述】:

我想使用 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 请求?

【问题讨论】:

    标签: c http libevent


    【解决方案1】:

    LibEvent 为此提供了分块功能。可以看代码示例like thisthis one

    我们可以看到in documentation 那些函数——在 start()/end() 之间循环的 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()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2020-06-21
      • 2015-11-29
      • 1970-01-01
      • 2019-06-03
      • 2012-05-16
      相关资源
      最近更新 更多