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.
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-XY%
(where XY represents the version) and end with %%EOF
possibly followed by a 0x0D and/or 0x0A. A PDF file may contain multiple %%EOF
markers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.