Linux使用evhttp实现一个简单的HttpServer

直接上代码。

mian.cpp

#include <event.h>
//for http
#include <evhttp.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/util.h>
#include "cJSON.h"
#include <unistd.h>
#include <iostream>
#include <cstring>
struct evhttp *m_phttpd;
void login_handler(struct evhttp_request *req, void *arg)
{
    //获取POST方法的数据
    size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
    char request_data[1024] = { 0 };
    char *post_data = (char *)EVBUFFER_DATA(req->input_buffer);
    cJSON *root = NULL;
    if (post_size > 0 && post_size < 1024)
    {
        memcpy(request_data, post_data, post_size);
        root = cJSON_Parse(request_data);
    }
    //给前段回复一个响应结果
    cJSON*response_root = cJSON_CreateObject();
    if (root && response_root)
    {
        cJSON* username_obj = cJSON_G
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现linux下的基于C语言的evhttp请求中一个完整的POST的功能,可以按照以下步骤进行: 1. 创建一个evhttp_connection对象,并设置连接的主机名和端口号。 2. 创建一个evhttp_request对象,并设置请求的方法为POST,请求的URI路径和查询参数。 3. 设置请求头部信息,包括Content-Type和Content-Length等。 4. 将POST请求的数据写入请求体中。 5. 发送POST请求并等待响应。 6. 处理响应结果,包括状态码、响应头部和响应体。 下面是一个示例代码,可以参考实现: ```c #include <event2/event.h> #include <event2/http.h> #include <event2/buffer.h> void http_request_done(struct evhttp_request *req, void *arg) { char *response = NULL; int response_len = 0; if (req == NULL) { printf("http request failed.\n"); return; } if (req->response_code != HTTP_OK) { printf("http response code: %d\n", req->response_code); return; } struct evbuffer *evbuf = evhttp_request_get_input_buffer(req); response_len = evbuffer_get_length(evbuf); if (response_len > 0) { response = (char*)malloc(response_len + 1); evbuffer_remove(evbuf, response, response_len); response[response_len] = 0; printf("http response: %s\n", response); } } int main(int argc, char **argv) { struct event_base *base = NULL; struct evhttp_connection *conn = NULL; struct evhttp_request *req = NULL; const char *host = "www.example.com"; const int port = 80; const char *path = "/api/test"; const char *data = "param1=value1&param2=value2"; base = event_base_new(); conn = evhttp_connection_base_new(base, NULL, host, port); req = evhttp_request_new(http_request_done, NULL); evhttp_add_header(req->output_headers, "Content-Type", "application/x-www-form-urlencoded"); evhttp_add_header(req->output_headers, "Content-Length", strlen(data)); evbuffer_add(req->output_buffer, data, strlen(data)); evhttp_make_request(conn, req, EVHTTP_REQ_POST, path); event_base_dispatch(base); if (req != NULL) { evhttp_request_free(req); } if (conn != NULL) { evhttp_connection_free(conn); } if (base != NULL) { event_base_free(base); } return 0; } ``` 这段代码演示了如何向www.example.com的/api/test路径发送一个POST请求,并传递参数param1=value1和param2=value2。在请求头部中设置了Content-Type和Content-Length信息,然后将请求数据写入请求体中。最后,通过调用event_base_dispatch函数等待响应结果,并在回调函数http_request_done中处理响应结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值