[英]How to get download size in Poco::HTTPClientSession?
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()
: 您可以使用
HTTPResponse::getContentLength()
或getContentLength64()
:
Returns the content length for this message, which may be UNKNOWN_CONTENT_LENGTH if no Content-Length header is present.
返回此消息的内容长度,如果不存在Content-Length标头,则可能是UNKNOWN_CONTENT_LENGTH。
Note, that you have to call HTTPClientSession::receiveResponse()
first to receive the headers. 注意,您必须首先调用
HTTPClientSession::receiveResponse()
来接收标头。 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. 您可能还需要检查
HTTPResponse::getStatus()
以验证请求是否正常,如果您期望特定类型,则需要检查HTTPResponse::getContentType()
。
For PDFs you can check the begin/end markers of the file itself. 对于PDF,您可以检查文件本身的开始/结束标记。 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. 正确的PDF文件应以
%PDF-XY%
(其中XY表示版本)开头,并以%%EOF
结尾,可能后跟0x0D和/或0x0A。 A PDF file may contain multiple %%EOF
markers. PDF文件可能包含多个
%%EOF
标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.