Skip to content

Commit 1dabac6

Browse files
committed
rework HTTPclient examples
1 parent e15c745 commit 1dabac6

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,28 @@ void loop() {
4343

4444
USE_SERIAL.print("[HTTP] begin...\n");
4545
// configure traged server and url
46-
//http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
47-
http.begin("192.168.1.12", 80, "/test.html"); //HTTP
46+
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
47+
http.begin("http://192.168.1.12/test.html"); //HTTP
4848

4949
USE_SERIAL.print("[HTTP] GET...\n");
5050
// start connection and send HTTP header
5151
int httpCode = http.GET();
52+
53+
// httpCode will be negative on error
5254
if(httpCode) {
5355
// HTTP header has been send and Server response header has been handled
5456
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5557

5658
// file found at server
57-
if(httpCode == 200) {
59+
if(httpCode == HTTP_CODE_OK) {
5860
String payload = http.getString();
5961
USE_SERIAL.println(payload);
6062
}
6163
} else {
6264
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
6365
}
66+
67+
http.end();
6468
}
6569

6670
delay(10000);

libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,31 @@ void setup() {
3636

3737
WiFiMulti.addAP("SSID", "PASSWORD");
3838

39-
39+
// allow reuse (if server supports it)
40+
http.setReuse(true);
4041
}
4142

4243
void loop() {
4344
// wait for WiFi connection
4445
if((WiFiMulti.run() == WL_CONNECTED)) {
4546

46-
http.begin("192.168.1.12", 80, "/test.html");
47+
http.begin("http://192.168.1.12/test.html");
48+
//http.begin("192.168.1.12", 80, "/test.html");
4749

4850
int httpCode = http.GET();
4951
if(httpCode) {
5052
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5153

5254
// file found at server
53-
if(httpCode == 200) {
55+
if(httpCode == HTTP_CODE_OK) {
5456
http.writeToStream(&USE_SERIAL);
5557
}
5658
} else {
5759
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
5860
}
61+
62+
http.end();
63+
5964
}
6065

6166
delay(1000);

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ void loop() {
4242
HTTPClient http;
4343

4444
USE_SERIAL.print("[HTTP] begin...\n");
45-
// configure traged server and url
46-
http.begin("192.168.1.12", 80, "/test.html");
45+
46+
// configure server and url
47+
http.begin("http://192.168.1.12/test.html");
48+
//http.begin("192.168.1.12", 80, "/test.html");
4749

4850
USE_SERIAL.print("[HTTP] GET...\n");
4951
// start connection and send HTTP header
5052
int httpCode = http.GET();
5153
if(httpCode) {
5254
// HTTP header has been send and Server response header has been handled
53-
5455
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5556

5657
// file found at server
57-
if(httpCode == 200) {
58+
if(httpCode == HTTP_CODE_OK) {
5859

5960
// get lenght of document (is -1 when Server sends no Content-Length header)
6061
int len = http.getSize();
@@ -91,6 +92,8 @@ void loop() {
9192
} else {
9293
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
9394
}
95+
96+
http.end();
9497
}
9598

9699
delay(10000);

0 commit comments

Comments
 (0)