You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.0 KiB

  1. 
  2. #include <iostream>
  3. #include<event2/event.h>
  4. #include <event2/listener.h>
  5. #include <event2/http.h>
  6. #include <event2/listener.h>
  7. #include <event2/buffer.h>
  8. #include <event2/bufferevent.h>
  9. #include <string.h>
  10. #ifndef _WIN32
  11. #include <signal.h>
  12. #endif // !_WIN32
  13. using namespace std;
  14. void http_client_cb(struct evhttp_request *req, void *ctx) {
  15. cout << "http_client_cb" << endl;
  16. bufferevent *bev = (bufferevent*)ctx;
  17. if (req == NULL) {
  18. int errcode = EVUTIL_SOCKET_ERROR();
  19. cout << "socket error: " << evutil_socket_error_to_string(errcode) << endl;
  20. return;
  21. }
  22. //获取path
  23. const char* path = evhttp_request_get_uri(req);
  24. cout << "request path is " << path << endl;
  25. //获取返回的code
  26. cout << "Respone: " << evhttp_request_get_response_code(req);
  27. cout << "" << evhttp_request_get_response_code_line(req) << endl;
  28. char buf[1024] = { 0 };
  29. evbuffer *input = evhttp_request_get_input_buffer(req);
  30. for (;;) {
  31. int len = evbuffer_remove(input,buf,sizeof(buf)-1);
  32. if(len <=0 )
  33. buf[len] = 0;
  34. }
  35. }
  36. int main()
  37. {
  38. #ifdef _WIN32
  39. //初始化socket库
  40. WSADATA wsa;
  41. WSAStartup(MAKEWORD(2, 2), &wsa);
  42. #else
  43. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉!
  44. return 1;
  45. }
  46. #endif
  47. //创建libevent上下文
  48. event_base* base = event_base_new();
  49. // 生成请求信息 GET
  50. //string http_url = "http://www.pyfup.com/index.html?id=1";
  51. string http_url = "http://121.4.70.4:3000//index.html?id=1"; //地址 自己换着测试
  52. evhttp_uri *uri = evhttp_uri_parse(http_url.c_str());
  53. const char* schemo = evhttp_uri_get_scheme(uri);
  54. if (!schemo) {
  55. cerr << "schemo is null" << endl;
  56. return -1;
  57. }
  58. cout << "schemo is " << schemo << endl;
  59. int port = evhttp_uri_get_port(uri);
  60. if (port < 0) {
  61. if (strcmp(schemo, "http") == 0)
  62. port = 80;
  63. }
  64. cout << "host is " << port << endl;
  65. const char* host = evhttp_uri_get_host(uri);
  66. if (!host) {
  67. cerr << "host is null" << endl;
  68. return -1;
  69. }
  70. cout << "host is " << host << endl;
  71. const char *path = evhttp_uri_get_path(uri);
  72. if (!path || strlen(path) == 0) {
  73. path = "/";
  74. }
  75. cout << "path is " << path << endl;
  76. //?号之后的内容
  77. const char* query = evhttp_uri_get_query(uri);
  78. if (query) {
  79. cout << "query is " << query << endl;
  80. }
  81. bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
  82. evhttp_connection *evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port);
  83. evhttp_request *req = evhttp_request_new(http_client_cb, bev);
  84. //设置请求的head 消息报头 信息
  85. struct evkeyvalq*out_put_headers = evhttp_request_get_output_headers(req);
  86. evhttp_add_header(out_put_headers, "Host", host);
  87. //发起请求
  88. evhttp_make_request(evcon, req, EVHTTP_REQ_GET, path);
  89. if (base)
  90. event_base_dispatch(base); //事件分发处理
  91. if (base)
  92. event_base_free(base);
  93. #if _WIN32
  94. WSACleanup();
  95. #endif // _WIN32
  96. return 0;
  97. }