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.

61 line
1.9KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <string.h>
  11. #include "mk_mediakit.h"
  12. typedef struct {
  13. mk_sem sem;
  14. mk_http_requester requester;
  15. } Context;
  16. void API_CALL on_requester_complete(void *user_data, int code, const char *err_msg){
  17. Context *ctx = (Context *)user_data;
  18. log_debug("code: %d %s", code, err_msg);
  19. size_t res_len = 0;
  20. log_debug("response: %s %s", mk_http_requester_get_response_status(ctx->requester),
  21. mk_http_requester_get_response_body(ctx->requester, &res_len));
  22. mk_sem_post(ctx->sem, 1);
  23. }
  24. int main(int argc, char *argv[]) {
  25. mk_config config = {
  26. .ini = NULL,
  27. .ini_is_path = 0,
  28. .log_level = 0,
  29. .log_mask = LOG_CONSOLE,
  30. .ssl = NULL,
  31. .ssl_is_path = 1,
  32. .ssl_pwd = NULL,
  33. .thread_num = 0
  34. };
  35. mk_env_init(&config);
  36. mk_http_requester requester = mk_http_requester_create();
  37. mk_http_requester_set_method(requester, "POST");
  38. mk_http_body body = mk_http_body_from_string("tn=monline_7_dg&ie=utf-8&wd=test", 0);
  39. mk_http_requester_set_body(requester, body);
  40. mk_http_body_release(body);
  41. mk_sem sem = mk_sem_create();
  42. Context ctx = {.requester = requester, .sem = sem};
  43. mk_http_requester_set_cb(requester, on_requester_complete, &ctx);
  44. mk_http_requester_start(requester, "http://www.baidu.com/baidu", 10);
  45. //等待http请求完毕
  46. mk_sem_wait(sem);
  47. mk_sem_release(sem);
  48. mk_http_requester_release(requester);
  49. return 0;
  50. }