如何使用 NODEMCU 发送 HTTPS GET 请求

How to send HTTPS GET request using NODEMCU

如何使用 Nodemcu - Arduino 代码发送 HTTPS - GET/POST 请求。

我一直在寻找一个使用 HTTPS 协议向网站发送 GET 请求的工作示例,但我发现的所有示例都不成功。

希望对其他人也有帮助!

Here is the link that helped me

要使示例使用 HTTPS 协议,您必须使用 WiFiClientSecure 库并调用您拥有的 WiFiClientSecure 对象的 client.setInsecure() 函数。

这是一个完整的工作代码:

#include <ESP8266WiFi.h>

const char* ssid     = "WIFI_SSID";
const char* password = "WIFI_PASS";

const char* host = "DOMAIN_NAME"; // only google.com not https://google.com

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClientSecure client;
  const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
  
  client.setInsecure(); // this is the magical line that makes everything work
  
  if (!client.connect(host, httpPort)) { //works!
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/arduino.php";
  url += "?data=";
  url += "aaaa";


  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

  Serial.println();
  Serial.println("closing connection");
}