位置导航: ESP8266库 / WiFiClientSecure库 / 本页
说明
使用allowSelfSignedCerts将会允许ESP8266对使用自签名证书的服务器进行身份认证。
服务器证书需要由权威证书颁发机构签名方可信赖。自签名证书不具安全效力,仅可用于测试使用。
语法
httpsClient.allowSelfSignedCerts()
参数
无
返回值
无
示例程序
请将以下示例程序中的Wifi联网信息进行调整并将程序上传给ESP8266。程序上传完毕后,ESP8266将会自动连接WiFi并通过HTTPS协议与badssl.com网站服务器进行通讯。
由于在以下示例程序中使用了httpsClient.allowSelfSignedCerts(),ESP8266对使用自签名证书的服务器进行身份认证,服务器证书需要由权威证书颁发机构签名方可信赖。自签名证书不具安全效力,仅可用于测试使用。所以此方法不适合传输需要保密的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/********************************************************************** 项目名称/Project : 零基础入门学用物联网 程序名称/Program name : WiFiClientSecure_allowSelfSignedCerts 团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com) 作者/Author : CYNO朔 日期/Date(YYYYMMDD) : 20200418 程序目的/Purpose : 此程序用于演示如何使用WiFiClientSecure库中的allowSelfSignedCerts来控制ESP8266 的HTTPS协议通讯。使用allowSelfSignedCerts将会让ESP8266允许使用自签名证书进行服务器认证。 ----------------------------------------------------------------------- 本示例程序为太极创客团队制作的《零基础入门学用物联网》中示例程序。 该教程为对物联网开发感兴趣的朋友所设计和制作。如需了解更多该教程的信息,请参考以下网页: http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/ ***********************************************************************/ #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> const char *ssid = "taichimaker"; const char *pass = "12345678"; // 连接服务器 void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) { if (!path) { path = "/"; } Serial.printf("Trying: %s:443...", host); client->connect(host, port); if (!client->connected()) { Serial.printf("*** Can't connect. ***\n-------\n"); return; } Serial.printf("Connected!\n-------\n"); // 向服务器发送请求 client->write("GET "); client->write(path); client->write(" HTTP/1.0\r\nHost: "); client->write(host); client->write("\r\nUser-Agent: ESP8266\r\n"); client->write("\r\n"); uint32_t to = millis() + 5000; if (client->connected()) { do { char tmp[32]; memset(tmp, 0, 32); int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1); yield(); if (rlen < 0) { break; } // 输出服务器响应状态信息 char *nl = strchr(tmp, '\r'); if (nl) { *nl = 0; Serial.print(tmp); break; } Serial.print(tmp); } while (millis() < to); } client->stop(); } // 通过允许自签名证书方式连接网站服务器 void fetchSelfSigned() { BearSSL::WiFiClientSecure client; // 现尝试不开启自签名认证连接服务器(服务器会拒绝连接) Serial.printf("First, try and connect to a badssl.com self-signed website (will fail):\n"); //设置允许自签名证书 fetchURL(&client, "self-signed.badssl.com", 443, "/"); // 开启自签名认证连接服务器(可正常连接服务器) Serial.printf("Now we'll enable self-signed certs (will pass)\n"); client.allowSelfSignedCerts(); fetchURL(&client, "self-signed.badssl.com", 443, "/"); } void setup() { Serial.begin(9600); Serial.println(); // 连接WiFi Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); fetchSelfSigned(); } void loop() {} |
位置导航: ESP8266库 / WiFiClientSecure库 / 本页