我正在做一个项目,三天以来我遇到了一个问题。我正在使用 ESP32 做这个项目,并希望将其自动连接到 WIFI 并获取 API 信息。第一部分没问题,我做到了没有任何问题。但它没有获取 API 信息。我的 API-Link 是这样的:
http://test.de/milad/api/Tv
它需要一个用户和密码:
User: Demo
Pass: OIMLyyIR03QZTqN2KFZkDKneuuk5ixMgeGOHS7r3
我试图阅读互联网上的所有 BASIC-Auth 示例。但我找不到解决方案...我收到此错误:
[HTTP] GET... failed, error: connection refused
例如,我使用了以下代码:
// wait for WiFi connection
if((wifiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("http://test.de/milad/api/Tv");
http.setAuthorization("demo", "OIMLyyIR03QZTqN2KFZkDKneuuk5ixMgeGOHS7r3");
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();