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.

112 lines
2.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: client_get.c,v 1.1 2004/10/15 13:42:07 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C (example)
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #define MEM_DEBUG
  25. #include <stdio.h>
  26. #include <nanohttp/nanohttp-client.h>
  27. #ifdef MEM_DEBUG
  28. #include <utils/alloc.h>
  29. #endif
  30. #define MAX_BUFFER_SIZE 1024
  31. static
  32. void show_response(hresponse_t *res)
  33. {
  34. hpair_t *pair;
  35. byte_t buffer[MAX_BUFFER_SIZE+1];
  36. int read;
  37. if (res == NULL)
  38. {
  39. log_error1("Response is NULL!");
  40. return;
  41. }
  42. log_info2("Status: %d", res->errcode);
  43. log_info2("Desc : '%s'", res->desc);
  44. pair = res->header;
  45. while (pair != NULL) {
  46. log_debug3("%s: %s", pair->key, pair->value);
  47. pair = pair->next;
  48. }
  49. if (res->in == NULL)
  50. {
  51. log_warn1("No input stream!");
  52. return;
  53. }
  54. while (http_input_stream_is_ready(res->in))
  55. {
  56. read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE);
  57. buffer[read] = '\0';
  58. puts(buffer);
  59. }
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. httpc_conn_t *conn; /* Client connection object */
  64. hresponse_t *res; /* Response object **/
  65. /* check usage */
  66. if (argc < 2) {
  67. fprintf(stderr, "usage %s <url>\n", argv[0]);
  68. exit(1);
  69. }
  70. /* Set log level to see more information written by the library */
  71. log_set_level(HLOG_VERBOSE);
  72. /* Initialize httpc module */
  73. if (httpc_init(argc, argv))
  74. {
  75. log_error1("Can not init httpc");
  76. return 1;
  77. }
  78. /* Create the client connection object */
  79. conn = httpc_new();
  80. /* Send GET method and receive response */
  81. res = httpc_get(conn, argv[1]);
  82. /* Show response */
  83. show_response(res);
  84. /* Clean up */
  85. hresponse_free(res);
  86. httpc_free(conn);
  87. #ifdef MEM_DEBUG
  88. _mem_report();
  89. #endif
  90. return 0;
  91. }