ESP8266 HTTPS 获取失败

问题描述

我正在尝试使用 HTTPS 连接到一个站点。 我编写了代码并且连接有效。但是服务器回复我 我要查找的文件PHP 文件)已移至 https://...

位置是正确的,但在我看来,ESP8266 发送 GET 请求时没有使用 HTTPS,而仅使用 HTTP。 是否有可能? 这是服务器的答案:

> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
> <title>301 Moved Permanently</title> </head><body> <h1>Moved
> Permanently</h1> <p>The document has moved <a
> href="https://mysite/myApp/nodmcu/selectParam_nodemcu_2.PHP?IdUte=57&amp;Campo=1">here</a>.</p>
> </body></html>

这里是代码

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>

const char* ssid = "MYWIFINET";
const char* password = "XXXXXXXXXXX";

const char* host = "mysite.org"; // 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 = "/Myapp/nodemcu/selectParam_nodemcu_2.PHP?IdUte=57&Campo=1";

  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("request sent");
  while (client.connected()) 
  {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)