2

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
Govinda P
  • 3,261
  • 5
  • 22
  • 43
tux-world
  • 2,680
  • 6
  • 24
  • 55

1 Answers1

0

When you are getting 200 (OK) instead of 206 (Partial Content), your Range Retrieval Requests is not satisfied.

From W3 docs :

  • The presence of a Range header in an unconditional GET modifies what is returned if the GET is otherwise successful. In other words, the response carries a status code of 206 (Partial Content) instead of 200 (OK).

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.

Community
  • 1
  • 1
Monish Kamble
  • 1,478
  • 1
  • 15
  • 28
  • i dont have any access to server, this link can be downloaded via other application such as whatsApp or telegram without any problem, i'm wondering why this code can't get file length. could you help me to fix code? – tux-world Dec 30 '16 at 05:11
  • I am unable to access the URL due to my office firewall. I'll try some other ways and let you know. Have you checked the response header for `Accept-Ranges` as I mentioned in the edit. – Monish Kamble Dec 30 '16 at 05:21