1 year ago
#381230
Salman
Is there any way to get content-length without getting response body
I am using Apache HttpClient to download content from the server. I want to check the size before downloading content. I have used HTTPGet to download content and before processing the inputstream I am checking the content-length header but I see HTTPGet will download the body too even though we do not process.
var httpGet = httpGet(url);
try (final var closeableHttpResponse = closeableHttpClient.execute(httpGet);) {
final var statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
final var headers = getHeadersMap(closeableHttpResponse.getAllHeaders());
final var contentLength = Long.valueOf(headers.get("content-length"));
final var entity = closeableHttpResponse.getEntity();
if (<<contentLength validation>>)) {
// Not to process
} else {
final var inputstream = entity.getContent();
// Processing inputstream
EntityUtils.consumeQuietly(entity);
}
} catch (IOException e) {
// Error Handling
}
- If I use HTTP Head it will give only headers. Is it a correct approach to determining response size?
- There are some cases in which the content-length header doesn't have length. Is there any other way too for determine size before downloading if the content-length header doesn't have a length?
java
http
apache-httpclient-4.x
0 Answers
Your Answer