httpclient_sample.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * File : httpclient.c
  3. *
  4. * Copyright (c) 2006-2018, RT-Thread Development Team
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2018-07-20 flybreak first version
  11. */
  12. #include <webclient.h> /* 使用 HTTP 协议与服务器通信需要包含此头文件 */
  13. #include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
  14. #include <netdb.h>
  15. #include <cJSON.h>
  16. #include <finsh.h>
  17. #define BUF_SZ 4096 //缓冲区大小
  18. #define URL_LEN_MAX 256 //网址最大长度
  19. #define AREA_ID "101021300" //上海浦东地区 ID
  20. /* 天气数据解析 */
  21. void weather_data_parse(rt_uint8_t *data)
  22. {
  23. cJSON *root = RT_NULL, *object = RT_NULL, *item = RT_NULL;
  24. root = cJSON_Parse((const char *)data);
  25. if (!root)
  26. {
  27. rt_kprintf("No memory for cJSON root!\n");
  28. return;
  29. }
  30. object = cJSON_GetObjectItem(root, "sk_info");
  31. item = cJSON_GetObjectItem(object, "cityName");
  32. rt_kprintf("\ncityName:%s ", item->valuestring);
  33. item = cJSON_GetObjectItem(object, "temp");
  34. rt_kprintf("\ntemp :%s ", item->valuestring);
  35. item = cJSON_GetObjectItem(object, "wd");
  36. rt_kprintf("\nwd :%s ", item->valuestring);
  37. item = cJSON_GetObjectItem(object, "ws");
  38. rt_kprintf("\nws :%s ", item->valuestring);
  39. item = cJSON_GetObjectItem(object, "sd");
  40. rt_kprintf("\nsd :%s ", item->valuestring);
  41. item = cJSON_GetObjectItem(object, "date");
  42. rt_kprintf("\ndate :%s", item->valuestring);
  43. item = cJSON_GetObjectItem(object, "time");
  44. rt_kprintf("\ntime :%s \n", item->valuestring);
  45. if (root != RT_NULL)
  46. cJSON_Delete(root);
  47. }
  48. void weather(int argc, char **argv)
  49. {
  50. rt_uint8_t *ptr = RT_NULL;
  51. int length = 0, result;
  52. struct webclient_session *session = RT_NULL;
  53. char *weather_url = RT_NULL;
  54. /* 为 weather_url 分配空间 */
  55. weather_url = rt_calloc(1, URL_LEN_MAX);
  56. if (!weather_url)
  57. {
  58. rt_kprintf("No memory for weather_url!\n");
  59. goto __exit;
  60. }
  61. /* 拼接 GET 网址 */
  62. rt_snprintf(weather_url, URL_LEN_MAX, "http://mobile.weather.com.cn/data/sk/%s.html", AREA_ID);
  63. /* 为结构体 webclient_session 分配空间 */
  64. session = (struct webclient_session *)rt_calloc(1, sizeof(struct webclient_session));
  65. if (!session)
  66. {
  67. rt_kprintf("No memory for session structure!\n");
  68. goto __exit;
  69. }
  70. /* 连接天气网站 */
  71. result = webclient_connect(session, weather_url);
  72. if (result < 0)
  73. {
  74. rt_kprintf("Webclient connect URI(%s) failed!\n", weather_url);
  75. goto __exit;
  76. }
  77. /* 发送官方标准 header */
  78. result = webclient_send_header(session, WEBCLIENT_GET, RT_NULL, RT_NULL);
  79. if (result < 0)
  80. {
  81. rt_kprintf("Webclient send header buffer failed return %d!", result);
  82. goto __exit;
  83. }
  84. /* 检查响应 */
  85. if (webclient_handle_response(session))
  86. {
  87. if (session->response != 200)
  88. {
  89. rt_kprintf("webclient handle response(%d) error!", session->response);
  90. goto __exit;
  91. }
  92. }
  93. /* 分配用于存放接收数据的缓冲 */
  94. ptr = rt_calloc(1, BUF_SZ);
  95. if(!ptr)
  96. {
  97. rt_kprintf("No memory for data receive buffer!\n");
  98. goto __exit;
  99. }
  100. length = webclient_read(session, ptr, BUF_SZ);
  101. ptr[length] = '\0';
  102. /* 天气数据解析 */
  103. weather_data_parse(ptr);
  104. __exit:
  105. /* 释放网址空间 */
  106. if (weather_url != RT_NULL)
  107. rt_free(weather_url);
  108. /* 关闭会话 */
  109. if (session != RT_NULL)
  110. webclient_close(session);
  111. /* 释放缓冲区空间 */
  112. if (ptr != RT_NULL)
  113. rt_free(ptr);
  114. }
  115. MSH_CMD_EXPORT(weather, Get weather by webclient);