Skip to content

Instantly share code, notes, and snippets.

@azat
Last active May 18, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/0dc73fe42bfac33d80f767657ea5f99e to your computer and use it in GitHub Desktop.
Save azat/0dc73fe42bfac33d80f767657ea5f99e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <evhttp.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/bufferevent.h>
void http_request_done(struct evhttp_request *req, void *arg){
char buf[1024];
int s = evbuffer_remove(req->input_buffer, &buf, sizeof(buf) - 1);
buf[s] = '\0';
printf("%s", buf);
// terminate event_base_dispatch()
event_base_loopbreak((struct event_base *)arg);
}
int main(int argc, char **argv){
struct event_base *base;
struct evhttp_connection *conn;
struct evhttp_request *req;
base = event_base_new();
conn = evhttp_connection_base_new(base, NULL, "127.0.0.1", 8080);
req = evhttp_request_new(http_request_done, base);
/* Tell libevent to free the connection when the request finishes */
evhttp_connection_free_on_completion(conn);
evhttp_add_header(req->output_headers, "Host", "localhost");
//evhttp_add_header(req->output_headers, "Connection", "close");
evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/index.php?id=1");
evhttp_connection_set_timeout(req->evcon, 600);
event_base_dispatch(base);
#if 1
event_base_free(base);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment