ESP32 Learn

HTTP Client: GET/POST with Retry

2025-09-18 路 ~1 min read
Table of contents
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <WiFi.h>

#include <HTTPClient.h>

String get(String url, int maxTry = 3) {
  HTTPClient http;
  for (int i = 0; i < maxTry; i++) {
    http.begin(url);
    int code = http.GET();
    if (code == 200) {
      String body = http.getString();
      http.end();
      return body;
    }
    http.end();
    delay(500 * (i + 1));
  }
  return "";
}
void setup() {
  Serial.begin(115200); /* connect WiFi */
  Serial.println(get("http://example.com"));
}
void loop() {}

馃敄 #http #client #retry