HttpClient
HttpClient copied to clipboard
not all content is downloaded
I try to download a picture from web server and save to SD. First what I see is a negative content length. Content length is: -16328 Way?
The picture content starts with 0x42, 0x4D .... on start index 0. ID: 0 0x42 ID: 1 0x4D .... so far so good....
the while loop stops on index 49047 but the full size is 49207
Whats wrong with your example code?
here my code
if (responseCode == 200) {
responseCode = http.skipResponseHeaders();
if (responseCode >= 0) {
int bodyLen = http.contentLength();
Serial.print("Content length is: ");
Serial.println(bodyLen);
unsigned long timeoutStart = millis();
char c;
long i = 0;
// Whilst we haven't timed out & haven't reached the end of the body
while ( (http.connected() || http.available()) &&
((millis() - timeoutStart) < 30000) ) {
if (http.available()) {
c = http.read();
Serial.print("ID: ");
Serial.print(i);
Serial.print("\t");
Serial.print(c);
Serial.print("\t");
Serial.println(c, HEX);
i++;
myFile.print(c);
bodyLen--;
// We read something, reset the timeout counter
timeoutStart = millis();
}
else
{
// We haven't got any data, so let's pause to allow some to arrive.
wait(1000);
}
}
if (!http.connected()) {
Serial.println("disconnecting.");
http.stop();
myFile.close();
Serial.println("Finished writing to file");
}
}
}