I'm trying to develop simple android download manager, in this library i get file length from http connection via two method as this code:
String contentLength = httpConnection.getHeaderField("Content-Length");
if (TextUtils.isEmpty(contentLength) || contentLength.equals("0") || contentLength.equals("-1")) {
length = httpConnection.getContentLength();
} else {
length = Long.parseLong(contentLength);
}
this code work fine when i get 206
on
httpConnection.getResponseCode();
but when i get 200
and
if (TextUtils.isEmpty(contentLength) || contentLength.equals("0") || contentLength.equals("-1")) {
is false
httpConnection.getContentLength();
return 0
full this part of my code:
try {
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(Constants.HTTP.CONNECT_TIME_OUT);
httpConnection.setReadTimeout(Constants.HTTP.READ_TIME_OUT);
httpConnection.setRequestMethod(Constants.HTTP.GET);
httpConnection.setRequestProperty("Range", "bytes=" + 0 + "-");
final int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
parseResponse(httpConnection, false);
} else if (responseCode == HttpURLConnection.HTTP_PARTIAL) {
parseResponse(httpConnection, true);
} else {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "UnSupported response code:" + responseCode);
}
} catch (ProtocolException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Protocol error", e);
} catch (IOException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "IO error", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
this is my testing link to download and get file length
http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-16.jpg
When you are getting 200 (OK) instead of 206 (Partial Content), your Range Retrieval Requests is not satisfied.
From W3 docs :
More details : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2
You must check server-side logs to know the reason behind its failure.
Edit1
When you are getting 200 (OK), check for
Accept-Ranges: bytes
header in Response header.Accept-Ranges
response header indicates that the server has range header support.Check here the exchange of Request-Response between Chrome and a static web server explained by johnstok.