Skip to content

Commit bf1f107

Browse files
author
Jeroen88
committed
Because of git problems, start from a new fork and create a new PR. This was PR esp8266#6457
1 parent b6e5830 commit bf1f107

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
reuseConnectionV2.ino
3+
4+
Created on: 22.11.2015
5+
6+
This example reuses the http connection and also restores the connection if the connection is lost
7+
*/
8+
9+
10+
#include <Arduino.h>
11+
12+
#include <ESP8266WiFi.h>
13+
#include <ESP8266WiFiMulti.h>
14+
15+
#include <ESP8266HTTPClient.h>
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
HTTPClient http;
20+
WiFiClient client;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println("Connecting to WiFi...");
30+
31+
WiFi.mode(WIFI_STA);
32+
WiFiMulti.addAP("SSID", "PASSWORD");
33+
34+
// wait for WiFi connection
35+
while ((WiFiMulti.run() != WL_CONNECTED)) {
36+
Serial.write('.');
37+
delay(500);
38+
}
39+
Serial.println(" connected to WiFi");
40+
41+
// allow reuse (if server supports it)
42+
http.setReuse(true);
43+
44+
45+
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
46+
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
47+
}
48+
49+
void loop() {
50+
for (int i = 0; i < 10; i++) {
51+
Serial.printf("Reuse connection example, GET url for the %d time\n", i + 1);
52+
int httpCode = http.GET();
53+
if (httpCode > 0) {
54+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
55+
56+
// file found at server
57+
if (httpCode == HTTP_CODE_OK) {
58+
http.writeToStream(&Serial);
59+
}
60+
} else {
61+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
62+
// Something went wrong with the connection, try to reconnect
63+
http.end();
64+
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
65+
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
66+
}
67+
68+
Serial.println("\n\n\nWait 5 second...\n");
69+
delay(5000);
70+
}
71+
72+
http.end();
73+
74+
Serial.println("Done testing, now wait forever");
75+
for (;;) delay(100); // Wait forever
76+
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ void HTTPClient::disconnect(bool preserveClient)
456456
#endif
457457
}
458458
} else {
459+
if (!preserveClient && _client) { // Also destroy _client if not connected()
460+
_client = nullptr;
461+
}
462+
459463
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");
460464
}
461465
}
@@ -922,7 +926,9 @@ int HTTPClient::writeToStream(Stream * stream)
922926
return returnError(HTTPC_ERROR_NO_STREAM);
923927
}
924928

925-
if(!connected()) {
929+
// Only return error if not connected and no data available, because otherwise ::getString() will return an error instead of an empty
930+
// string when the server returned a http code 204 (no content)
931+
if(!connected() && _transferEncoding != HTTPC_TE_IDENTITY && _size > 0) {
926932
return returnError(HTTPC_ERROR_NOT_CONNECTED);
927933
}
928934

@@ -931,11 +937,13 @@ int HTTPClient::writeToStream(Stream * stream)
931937
int ret = 0;
932938

933939
if(_transferEncoding == HTTPC_TE_IDENTITY) {
934-
ret = writeToStreamDataBlock(stream, len);
940+
if(len > 0) {
941+
ret = writeToStreamDataBlock(stream, len);
935942

936-
// have we an error?
937-
if(ret < 0) {
938-
return returnError(ret);
943+
// have we an error?
944+
if(ret < 0) {
945+
return returnError(ret);
946+
}
939947
}
940948
} else if(_transferEncoding == HTTPC_TE_CHUNKED) {
941949
int size = 0;
@@ -1289,6 +1297,7 @@ int HTTPClient::handleHeaderResponse()
12891297
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
12901298
}
12911299
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
1300+
_canReuse = (_returnCode != '0');
12921301
} else if(headerLine.indexOf(':')) {
12931302
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
12941303
String headerValue = headerLine.substring(headerLine.indexOf(':') + 1);

0 commit comments

Comments
 (0)