Question

HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
session.setTimeout(Poco::Timespan(20,0));
session.sendRequest(req);
HTTPResponse res;
istream& rs = session.receiveResponse(res);

File saveTo(request.name,File::WriteOnly,true);
char aux_buffer[1024];
rs.read(aux_buffer, 1024);
std::streamsize n = rs.gcount();
while (n > 0){
    saveTo.write(aux_buffer,n);
    if (rs){
        rs.read(aux_buffer, 1024);
        n = rs.gcount();
    }
    else n = 0;
    }

}

How to get download size before i download it? Because sometimes i get corrupted file, so i want to check the size.

Was it helpful?

Solution

You can use HTTPResponse::getContentLength() or getContentLength64():

Returns the content length for this message, which may be UNKNOWN_CONTENT_LENGTH if no Content-Length header is present.

Note, that you have to call HTTPClientSession::receiveResponse() first to receive the headers. You may also want to check HTTPResponse::getStatus() to verify that the request was ok and HTTPResponse::getContentType() if you're expecting a specific type.

For PDFs you can check the begin/end markers of the file itself. A proper PDF file should begin with %PDF-X.Y% (where X.Y represents the version) and end with %%EOF possibly followed by a 0x0D and/or 0x0A. A PDF file may contain multiple %%EOF markers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top