日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ESP32 HTTP Client接口使用

發(fā)布時(shí)間:2023/12/16 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ESP32 HTTP Client接口使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

記錄下ESP32自帶的HTTP接口的使用, ESP HTTP模塊提供了豐富的API支持HTTP/HTTPS的請(qǐng)求,模塊位于esp-idf\components\esp_http_client中,其中Kconfig可修改默認(rèn)使能HTTPS和身份驗(yàn)證,在menuconfig也可以進(jìn)行配置

如有異議,請(qǐng)指正

HTTP簡(jiǎn)介

HTTP協(xié)議是Hyper Text Transfer Protocol超文本傳輸協(xié)議的縮寫,基于TCP傳輸層協(xié)議進(jìn)行通信,采用客戶端-服務(wù)器模型(C/S架構(gòu)),屬于應(yīng)用層協(xié)議
HTTP數(shù)據(jù)傳輸是透明的,明文傳輸涉及到通信安全時(shí),傳輸層上可套接TLS/SSL協(xié)議進(jìn)行加密,也就是HTTPS

特點(diǎn)
  • HTTP默認(rèn)使用80端口,HTTPS默認(rèn)使用443端口
  • 無(wú)狀態(tài),協(xié)議對(duì)于事務(wù)處理沒有記憶能力,每次都是一個(gè)新的連接,服務(wù)端不會(huì)記錄前后的請(qǐng)求信息(針對(duì)這個(gè)問題,引入Cookie將記錄加密存儲(chǔ)到客戶端中)
  • 無(wú)連接,限制每次連接只處理一個(gè)請(qǐng)求。服務(wù)器處理完客戶的請(qǐng)求,并收到客戶的應(yīng)答后,即斷開連接;(HTTP1.1版本支持持久連接的方法)
  • 媒體獨(dú)立,允許傳輸任意類型的數(shù)據(jù)對(duì)象,傳輸類型由Content-Type定義

API

接口可參考頭文件 esp-idf\components/esp_http_client/include/esp_http_client.h

esp_http_client_init

原型esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)

  • 配置http客戶端參數(shù),分配需要的資源,返回esp_http_client_handle_t數(shù)據(jù)結(jié)構(gòu)句柄,必須優(yōu)先調(diào)用
esp_http_client_config_t config = {.url = "https://www.howsmyssl.com", // url.cert_pem = howsmyssl_com_root_cert_pem_start,//證書};esp_http_client_handle_t client = esp_http_client_init(&config);
  • 傳遞配置形參,url成員必填,如果跳過(guò)證書可將元素skip_cert_common_name_check改為TRUE
/*** @brief HTTP configuration*/ typedef struct {const char *url; //url請(qǐng)求接口必須配置 /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */const char *host; //服務(wù)器域名或ip地址 /*!< Domain or IP as string */int port; //端口 http默認(rèn)80 https 默認(rèn)443 /*!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */const char *username; //用戶名,認(rèn)證使用 /*!< Using for Http authentication */const char *password; //用戶密碼,認(rèn)證使用 /*!< Using for Http authentication */esp_http_client_auth_type_t auth_type; //認(rèn)證方式 /*!< Http authentication type, see `esp_http_client_auth_type_t` */const char *path; //路徑 /*!< HTTP Path, if not set, default is `/` */const char *query; //請(qǐng)求參數(shù) /*!< HTTP query */const char *cert_pem; //證書 /*!< SSL server certification, PEM format as string, if the client requires to verify server */const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */esp_http_client_method_t method; //請(qǐng)求方式 post get /*!< HTTP Method */int timeout_ms; //請(qǐng)求超時(shí) /*!< Network timeout in milliseconds */bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */int max_redirection_count; /*!< Max number of redirections on receiving HTTP redirect status code, using default value if zero*/int max_authorization_retries; /*!< Max connection retries on receiving HTTP unauthorized status code, using default value if zero. Disables authorization retry if -1*/http_event_handle_cb event_handler; //可注冊(cè)回調(diào) /*!< HTTP Event Handle */esp_http_client_transport_t transport_type; // 傳輸方式 tcp ssl /*!< HTTP transport type, see `esp_http_client_transport_t` */int buffer_size; //接收緩存大小 /*!< HTTP receive buffer size */int buffer_size_tx; //發(fā)送緩存大小 /*!< HTTP transmit buffer size */void *user_data; //http用戶數(shù)據(jù) /*!< HTTP user_data context */bool is_async; //同步模式 /*!< Set asynchronous mode, only supported with HTTPS for now */bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */bool skip_cert_common_name_check; //跳過(guò)證書 /*!< Skip any validation of server certificate CN field */ } esp_http_client_config_t;
esp_http_client_perform

原型esp_err_t esp_http_client_perform(esp_http_client_handle_t client)

  • 進(jìn)行鏈路的連接與數(shù)據(jù)的傳輸,內(nèi)部包含完整的收發(fā)斷連的API調(diào)用
esp_http_client_open -> esp_http_client_write -> esp_http_client_fetch_headers -> esp_http_client_read (and option) esp_http_client_close

需要在配置esp_http_client_init后調(diào)用運(yùn)行

esp_err_t err = esp_http_client_perform(client);
esp_http_client_set_post_field/esp_http_client_get_post_field

原型esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len)
-通過(guò)post方式請(qǐng)求的數(shù)據(jù),傳入發(fā)送數(shù)據(jù)的緩存地址與長(zhǎng)度,必須在esp_http_client_perform之前調(diào)用
esp_http_client_get_post_field:為獲取當(dāng)前當(dāng)前post指向的數(shù)據(jù)緩存地址

esp_http_client_set_header/esp_http_client_get_header/esp_http_client_delete_header
  • 設(shè)置、獲取、刪除請(qǐng)求頭,可新增、刪除或根據(jù)應(yīng)用發(fā)送的數(shù)據(jù)類型來(lái)自定義請(qǐng)求文件類型來(lái)修改請(qǐng)求頭
esp_http_client_set_header(client, "HeaderKey", "HeaderValue");
esp_http_client_set_method
  • 設(shè)置HTTP請(qǐng)求方式;
//請(qǐng)求方式枚舉 typedef enum {HTTP_METHOD_GET = 0, /*!< HTTP GET Method */HTTP_METHOD_POST, /*!< HTTP POST Method */HTTP_METHOD_PUT, /*!< HTTP PUT Method */HTTP_METHOD_PATCH, /*!< HTTP PATCH Method */HTTP_METHOD_DELETE, /*!< HTTP DELETE Method */HTTP_METHOD_HEAD, /*!< HTTP HEAD Method */HTTP_METHOD_NOTIFY, /*!< HTTP NOTIFY Method */HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */HTTP_METHOD_COPY, /*!< HTTP COPY Method */HTTP_METHOD_MOVE, /*!< HTTP MOVE Method */HTTP_METHOD_LOCK, /*!< HTTP LOCK Method */HTTP_METHOD_UNLOCK, /*!< HTTP UNLOCK Method */HTTP_METHOD_PROPFIND, /*!< HTTP PROPFIND Method */HTTP_METHOD_PROPPATCH, /*!< HTTP PROPPATCH Method */HTTP_METHOD_MKCOL, /*!< HTTP MKCOL Method */HTTP_METHOD_MAX, } esp_http_client_method_t;esp_http_client_set_method(client, HTTP_METHOD_GET);//GET請(qǐng)求
esp_http_client_open

原型esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)

  • 打開連接,發(fā)送http請(qǐng)求頭,形參write_len為發(fā)送內(nèi)容數(shù)據(jù)長(zhǎng)度
esp_http_client_write
  • 寫數(shù)據(jù),調(diào)取esp_http_client_open成功后,可通過(guò)該接口進(jìn)行數(shù)據(jù)內(nèi)容的發(fā)送
esp_http_client_fetch_headers
  • 讀取數(shù)據(jù),自動(dòng)處理掉響應(yīng)頭并返回接收包長(zhǎng)度
esp_http_client_read
  • 讀取http接收的數(shù)據(jù)流,返回實(shí)際讀取長(zhǎng)度;返回-1發(fā)生錯(cuò)誤
esp_http_client_close
  • 關(guān)閉http連接鏈路,會(huì)保留所有http請(qǐng)求資源
esp_http_client_cleanup
  • 通過(guò)句柄esp_http_client關(guān)閉連接,釋放資源,必須在esp_http_client_init初始化后使用
esp_http_client_set_redirection
  • 設(shè)置重定向URL。當(dāng)從服務(wù)器接收到30x代碼時(shí),客戶端存儲(chǔ)服務(wù)器提供的重定向URL。該函數(shù)將當(dāng)前的網(wǎng)址設(shè)置為重定向,使客戶端能夠執(zhí)行重定向請(qǐng)求。
esp_http_client_add_auth
  • 在接收到HTTP狀態(tài)代碼401時(shí),可以調(diào)用該應(yīng)用編程接口來(lái)添加授權(quán)信息,調(diào)取前需清除下接收緩存;認(rèn)證方式有 BASIC 認(rèn)證(基本認(rèn)證);DIGEST 認(rèn)證(摘要認(rèn)證);SSL 客戶端認(rèn)證;FormBase 認(rèn)證(基于表單認(rèn)證)

struct esp_http_client_event

  • Http事件數(shù)據(jù)結(jié)構(gòu),在esp_http_client_config_t中配置對(duì)應(yīng)的回調(diào)函數(shù)
typedef struct esp_http_client_event {esp_http_client_event_id_t event_id; // 事件id /*!< event_id, to know the cause of the event */esp_http_client_handle_t client; //句柄 /*!< esp_http_client_handle_t context */void *data; //事件數(shù)據(jù)緩存 /*!< data of the event */int data_len; // 事件數(shù)據(jù)長(zhǎng)度 /*!< data length of data */void *user_data; //用戶數(shù)據(jù) /*!< user_data context, from esp_http_client_config_t user_data */char *header_key; //http頭密鑰 /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header key */char *header_value; // http請(qǐng)求頭 /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header value */ } esp_http_client_event_t;

事件Id

typedef enum {HTTP_EVENT_ERROR = 0, //當(dāng)執(zhí)行期間出現(xiàn)任何錯(cuò)誤時(shí),會(huì)發(fā)生此事件 /*!< This event occurs when there are any errors during execution */HTTP_EVENT_ON_CONNECTED, //HTTP連接到服務(wù)器 /*!< Once the HTTP has been connected to the server, no data exchange has been performed */HTTP_EVENT_HEADERS_SENT, //發(fā)送請(qǐng)求頭 /*!< After sending all the headers to the server */HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatabilityand will be deprecated in future versions esp-idf */HTTP_EVENT_ON_HEADER, //接收到響應(yīng)頭 /*!< Occurs when receiving each header sent from the server */HTTP_EVENT_ON_DATA, //接收到數(shù)據(jù) /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */HTTP_EVENT_ON_FINISH, //http會(huì)話完成 /*!< Occurs when finish a HTTP session */HTTP_EVENT_DISCONNECTED, //http斷開事件 /*!< The connection has been disconnected */ } esp_http_client_event_id_t;

代碼實(shí)例

可參考idf目錄下的test_http_client.c,路徑esp-idf\components\esp_http_client\test

實(shí)例

//事件回調(diào) static esp_err_t _http_event_handle(esp_http_client_event_t *evt) {switch(evt->event_id) {case HTTP_EVENT_ERROR://錯(cuò)誤事件ESP_LOGI(TAG, "HTTP_EVENT_ERROR");break;case HTTP_EVENT_ON_CONNECTED://連接成功事件ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");break;case HTTP_EVENT_HEADER_SENT://發(fā)送頭事件ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");break;case HTTP_EVENT_ON_HEADER://接收頭事件ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");printf("%.*s", evt->data_len, (char*)evt->data);break;case HTTP_EVENT_ON_DATA://接收數(shù)據(jù)事件ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);if (!esp_http_client_is_chunked_response(evt->client)) {printf("%.*s", evt->data_len, (char*)evt->data);}break;case HTTP_EVENT_ON_FINISH://會(huì)話完成事件ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");break;case HTTP_EVENT_DISCONNECTED://斷開事件ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");break;}return ESP_OK; }//http client配置 esp_http_client_config_t config = {.method = HTTP_METHOD_GET, //get請(qǐng)求.url = "https://www.baidu.com/", //請(qǐng)求url.event_handler = _http_event_handle,//注冊(cè)時(shí)間回調(diào) };//測(cè)試入口 void http_client_test(void) {esp_http_client_handle_t client = esp_http_client_init(&config);//初始化配置esp_err_t err = esp_http_client_perform(client);//執(zhí)行請(qǐng)求if(err == ESP_OK){ESP_LOGI(TAG, "Status = %d, content_length = %d",esp_http_client_get_status_code(client),//狀態(tài)碼esp_http_client_get_content_length(client));//數(shù)據(jù)長(zhǎng)度ret = esp_http_client_read(client, pReadBuf, 512);//讀取512數(shù)據(jù)內(nèi)容if(ret > 0){ESP_LOGI(TAG, "recv data = %d %s", ret, pReadBuf);//打印數(shù)據(jù)}}esp_http_client_cleanup(client);//斷開并釋放資源 }

請(qǐng)求響應(yīng)頭,內(nèi)容即為百度的Html的頁(yè)面

總結(jié)

樂鑫提高的接口比較完整,從服務(wù)器獲取數(shù)據(jù)可直接使用上面的例程,如果是其他請(qǐng)求方式,如POST可以通過(guò)接口esp_http_client_set_post_field設(shè)置發(fā)送的內(nèi)容數(shù)據(jù),可使用esp_http_client_perform自動(dòng)來(lái)完成收發(fā)的工作,通過(guò)事件回調(diào)可清晰跟蹤當(dāng)前的狀態(tài),有助于問題的調(diào)試

官方文檔

總結(jié)

以上是生活随笔為你收集整理的ESP32 HTTP Client接口使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。