我在用ArduinoIDE对我的ESP32进行编程,在HTTP GET上遇到了问题。我正在做什么。
这是我用于HTTP GET调用的代码。
static WiFiClient wifi;
HttpClient wlanHttp=HttpClient(wifi,"my.server.tld");
wlanHttp.get("/setpos.php?id=DEADBEEF"); // -> this fails with error code -1
wlanHttp.responseStatusCode(); // follow-up error -1
wlanHttp.stop();
有什么问题吗?
在ESp32(如果使用HTTPClient.h)的代码应该是这样的。
static WiFiClient wifi;
HttpClient wlanHttp;
wlanHttp.begin("http://my.server.tld/setpos.php?id=DEADBEEF"); //Specify the URL
int httpCode = wlanHttp.GET(); //Make the request
if(httpCode > 0) { //Check for the returning code
if(httpCode == HTTP_CODE_OK) {
// get payload with http.getString();
Serial.println(httpCode);
// Serial.println(payload);
}
else {
Serial.printf("[HTTP] GET... failed, error: %s\n", wlanHttp.errorToString(httpCode).c_str());
}
}
else {
Serial.println("Error on HTTP request");
}
wlanHttp.end(); //Free the resources