webclient_get_sample.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * File : webclient_get_sample.c
  3. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-08-03 chenyong the first version
  22. */
  23. #include <rtthread.h>
  24. #include <webclient.h>
  25. #define GET_HEADER_BUFSZ 1024
  26. #define GET_RESP_BUFSZ 1024
  27. #define GET_LOCAL_URI "http://www.rt-thread.com/service/rt-thread.txt"
  28. int webclient_get_test(int argc, char **argv)
  29. {
  30. struct webclient_session* session = RT_NULL;
  31. unsigned char *buffer = RT_NULL;
  32. char *URI = RT_NULL;
  33. int index, ret = 0;
  34. int bytes_read, resp_status;
  35. int content_length = -1;
  36. if (argc == 1)
  37. {
  38. URI = web_strdup(GET_LOCAL_URI);
  39. if(URI == RT_NULL)
  40. {
  41. rt_kprintf("no memory for create URI buffer.\n");
  42. return -1;
  43. }
  44. }
  45. else if (argc == 2)
  46. {
  47. URI = web_strdup(argv[1]);
  48. if(URI == RT_NULL)
  49. {
  50. rt_kprintf("no memory for create URI buffer.\n");
  51. return -1;
  52. }
  53. }
  54. else
  55. {
  56. rt_kprintf("webclient_get_test [URI] - webclient GET request test.\n");
  57. return -1;
  58. }
  59. buffer = (unsigned char *) web_malloc(GET_HEADER_BUFSZ);
  60. if (buffer == RT_NULL)
  61. {
  62. rt_kprintf("no memory for receive buffer.\n");
  63. ret = -RT_ENOMEM;
  64. goto __exit;
  65. }
  66. /* create webclient session and set header response size */
  67. session = webclient_session_create(GET_HEADER_BUFSZ);
  68. if (session == RT_NULL)
  69. {
  70. ret = -RT_ENOMEM;
  71. goto __exit;
  72. }
  73. /* send GET request by default header */
  74. if ((resp_status = webclient_get(session, URI)) != 200)
  75. {
  76. rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
  77. ret = -RT_ERROR;
  78. goto __exit;
  79. }
  80. rt_kprintf("webclient GET request response data :\n");
  81. content_length = webclient_content_length_get(session);
  82. if (content_length < 0)
  83. {
  84. rt_kprintf("webclient GET request type is chunked.\n");
  85. do
  86. {
  87. bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
  88. if (bytes_read <= 0)
  89. {
  90. break;
  91. }
  92. for (index = 0; index < bytes_read; index++)
  93. {
  94. rt_kprintf("%c", buffer[index]);
  95. }
  96. } while (1);
  97. rt_kprintf("\n");
  98. }
  99. else
  100. {
  101. int content_pos = 0;
  102. do
  103. {
  104. bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
  105. if (bytes_read <= 0)
  106. {
  107. break;
  108. }
  109. for (index = 0; index < bytes_read; index++)
  110. {
  111. rt_kprintf("%c", buffer[index]);
  112. }
  113. content_pos += bytes_read;
  114. } while (content_pos < content_length);
  115. rt_kprintf("\n");
  116. }
  117. __exit:
  118. if (session)
  119. {
  120. webclient_close(session);
  121. }
  122. if (buffer)
  123. {
  124. web_free(buffer);
  125. }
  126. if (URI)
  127. {
  128. web_free(URI);
  129. }
  130. return ret;
  131. }
  132. #ifdef FINSH_USING_MSH
  133. #include <finsh.h>
  134. MSH_CMD_EXPORT_ALIAS(webclient_get_test, web_get_test, web_get_test [URI] webclient GET request test);
  135. #endif /* FINSH_USING_MSH */