更新日期:2020-02-14
HTTP Client功能实现例程
本例程介绍如何使用 WebClient 软件包发送 HTTP 协议 GET 和 POST 请求,并且接收响应的数据。即,备通过 GET 请求方式从指定服务器中获取数据,之后通过 POST 请求方式上传一段数据到指定服务器,并接收服务器响应数据。
HTTP协议
HTTP (Hypertext Transfer Protocol)协议,即超文本传输协议,是互联网上应用最为广泛的一种网络协议,由于其简捷、快速的使用方式,适用于分布式和合作式超媒体信息系统。HTTP 协议是基于TCP/IP 协议的网络应用层协议。默认端口为 80 端口。协议最新版本是 HTTP 2.0,目前是用最广泛的是HTTP 1.1。
HTTP 协议是一种请求/响应式的协议。一个客户端与服务器建立连接之后,发送一个请求给服务器。服务器接收到请求之后,通过接收到的信息判断响应方式,并且给予客户端相应的响应,完成整个 HTTP数据交互流程。
源代码
#include <rtthread.h> #include <rtdevice.h> #include <wifi_config.h> #include <webclient.h> #define DBG_TAG "main" #define DBG_LVL DBG_LOG #include <rtdbg.h> #define HTTP_GET_URL "http://www.rt-thread.com/service/rt-thread.txt" #define HTTP_POST_URL "http://www.rt-thread.com/service/echo" static struct rt_semaphore net_ready; const char *post_data = "RT-Thread is an open source IoT operating system from China!"; void wlan_ready_handler(int event, struct rt_wlan_buff *buff, void *parameter); void wlan_station_disconnect_handler(int event, struct rt_wlan_buff *buff, void *parameter); int webclient_get_data(void); int webclient_post_data(void); int main(void) { int result = RT_EOK; /* 配置 wifi 工作模式 */ rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION); /* 初始化 wlan 自动连接功能 */ wlan_autoconnect_init(); /* 使能 wlan 自动连接功能 */ rt_wlan_config_autoreconnect(RT_TRUE); /* 创建 'net_ready' 信号量 */ result = rt_sem_init(&net_ready, "net_ready", 0, RT_IPC_FLAG_FIFO); if (result != RT_EOK) { return -RT_ERROR; } /* 注册 wlan 连接网络成功的回调,wlan 连接网络成功后释放 'net_ready' 信号量 */ rt_wlan_register_event_handler(RT_WLAN_EVT_READY, wlan_ready_handler, RT_NULL); /* 注册 wlan 网络断开连接的回调 */ rt_wlan_register_event_handler(RT_WLAN_EVT_STA_DISCONNECTED, wlan_station_disconnect_handler, RT_NULL); /* 等待 wlan 连接网络成功 */ result = rt_sem_take(&net_ready, RT_WAITING_FOREVER); if (result != RT_EOK) { LOG_E("Wait net ready failed!"); rt_sem_delete(&net_ready); return -RT_ERROR; } /* HTTP GET 请求发送 */ webclient_get_data(); /* HTTP POST 请求发送 */ webclient_post_data(); } /** * The callback of network ready event */ void wlan_ready_handler(int event, struct rt_wlan_buff *buff, void *parameter) { rt_sem_release(&net_ready); } /** * The callback of wlan disconected event */ void wlan_station_disconnect_handler(int event, struct rt_wlan_buff *buff, void *parameter) { LOG_I("disconnect from the network!"); } /* HTTP client download data by GET request */ int webclient_get_data(void) { unsigned char *buffer = RT_NULL; int length = 0; length = webclient_request(HTTP_GET_URL, RT_NULL, RT_NULL, &buffer); if (length < 0) { LOG_E("webclient GET request response data error."); return -RT_ERROR; } LOG_D("webclient GET request response data :"); LOG_D("%s", buffer); web_free(buffer); return RT_EOK; } /* HTTP client upload data to server by POST request */ int webclient_post_data(void) { unsigned char *buffer = RT_NULL; int length = 0; length = webclient_request(HTTP_POST_URL, RT_NULL, post_data, &buffer); if (length < 0) { LOG_E("webclient POST request response data error."); return -RT_ERROR; } LOG_D("webclient POST request response data :"); LOG_D("%s", buffer); web_free(buffer); return RT_EOK; } |
运行代码后效果图
注意:本例程需要先连接WiFi