如何解决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&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 举报,一经查实,本站将立刻删除。