用libevent evhttp流客户端请求体? [英] Stream client request body with libevent evhttp?

查看:2812
本文介绍了用libevent evhttp流客户端请求体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to stream a client POST request body with libevent and evhttp. I've found examples of sending requests with fixed bodies, but am not sure how to setup a a request with a body I will need to continuously write and update for an undetermined period. Is it possible to do this will libevent? My current baseline of code looks something like this:

#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请求?

How do I go about sending a POST request with a streaming body?

推荐答案

LibEvent为此设置了分块功能。你可以看到代码示例这样的 this one

LibEvent has chunked functions for this. You can see code examples like this and this one

我们可以看到在文档中这些功能 - 块( )在start()/ end()之间的循环中:`

We can see in documentation those functions - chunk() in loop between start()/end():`


evhttp_send_reply_start (struct evhttp_request * req)

evhttp_send_reply_start(struct evhttp_request *req)

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

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

evhttp_send_reply_end (struct evhttp_request * req)`。

evhttp_send_reply_end(struct evhttp_request *req)`.

这些是用于发送的,如果你需要获取传入的分块数据,那么 evhttp_request_set_chunked_cb()

Those are for sending, and if you need to get incoming chunked data there is evhttp_request_set_chunked_cb()

这篇关于用libevent evhttp流客户端请求体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆