#include <errno.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <event2 / event.h>
#include <event2
/ buffer.h>
#include <event2
/ http.h>
#include < event2 / http_struct.h> #include < event2 / keyvalq_struct.h> #include < event2 / listener.h>
#include <event2 / util.h>
#include <event2 / bufferevent.h>
extern
std :: string EncodeUtf8fromString(std :: string in); //文字列型を送信するときに使用されます
extern
std :: string DecodeUtf8fromString(std :: string in); //文字列型を受信するときに使用されます
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 \ n"、buf); // event_base_dispatch()を終了しますevent_base_loopbreak( (struct event_base *)arg); }
int main(int argc、char ** argv)
{ WSADATA wsa_data; WSAStartup(0x0201、&wsa_data);
//定义urlchar
* url = "http://10.10.180.208:8080/singleStation/transport/notice";
struct evhttp_uri * uri = evhttp_uri_parse(url);
if(!uri)
{ fprintf(stderr、 "URLの解析に失敗しました!\ n"); -1を返します。 } //初期化ベース structevent_base * base = event_base_new(); if(!base) { fprintf(stderr、 "イベントベースの作成に失敗しました!\ n"); 1を返します。 } // 获取ホストconstchar * host = evhttp_uri_get_host(uri); if(!host) { fprintf(stderr、 "ホストの解析に失敗しました!\ n"); 1を返します。 }
// mashurl
int port = evhttp_uri_get_port(uri);
if(port <0)port = 80;
const char * request_url = url;
const char * path = evhttp_uri_get_path(uri);
if(path == NULL || strlen(path)== 0)
{ request_url = "/"; } struct evhttp_connection * conn; conn = evhttp_connection_base_new(base、NULL、host、port); if(!conn) { fprintf(stderr、 "create evhttp connection failed!\ n"); 1を返します。 } //設定超時間 evhttp_connection_set_timeout(conn、600); evhttp_connection_set_retries(conn、-1); //创建リクエスト
struct evhttp_request * req;
req = evhttp_request_new(http_request_done、base);
//例外ヘッダー
evhttp_add_header(req-> output_headers、 "Host"、host);
evhttp_add_header(req-> output_headers、 "Content-Type"、 "application / json");
//创建
jsonchar * post_data = "{\" robot_ip \ ":\" 127.0.0.1 \ "、\" desc \ ":\" T试\ "、\" need_confirm \ ":1}";
std :: string data = EncodeUtf8fromString(post_data);
evbuffer_add(req-> output_buffer、data.data()、strlen(data.data()));
int result = evhttp_make_request(conn、req、EVHTTP_REQ_POST、request_url);
printf( "結果:%d \ n"、結果);
event_base_dispatch(base);
0を返します。
}