简体   繁体   中英

Why can't ESP8266 HTTP client receive data?

When I type the serverName into the browser, it returns a json object. But here, it Serial.print(payload) returns -1. How can I fix this?

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
}

Use HTTP instead of HTTPS.

If you want to use HTTPS, you will have to specify the server's certificate's SHA1 fingerprint. How to do that is easily googled.

Some libraries will allow something like client->setInsecure(); , but I wouldn't go there because, well, it's insecure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM