为什么Esp8266 HTTP客户端无法接收数据?

问题描述

当我在浏览器中键入 serverName 时,它将返回一个json对象。但是在这里,它 Serial.print(payload)返回-1。 我该如何解决?

void loop() {
   if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
     
      HTTPClient http;  //Declare an object of class HTTPClient
     
      http.begin(serverName);  //Specify request destination
      int httpCode = http.GET();                                                                  
      //Send the request
     
      if (httpCode > 0) { //Check the returning code
     
         String payload = http.getString();   //Get the request response payload
         Serial.println(payload);                     //Print the response payload
      }
      http.end();   //Close connection
   }
     
   delay(10000);    //Send a request every 30 seconds
}

解决方法

使用http代替https。

如果要使用https,则必须指定服务器证书的SHA1指纹。如何做到这一点很容易谷歌。

某些库将允许使用client->setInsecure();之类的东西,但是我不会去那里,因为很不安全。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...