Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient get add a byte range header?

Does anyone know of how to request byte ranges along with an HTTP request? I am looking to facilitate the resuming of downloads in our application by requesting a byte range of where the download left off and reading its InputStream from getContent().

I tried iterating over the headers but they are null. Source is below.

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

/**
 * @author Kevin Kowalewski
 *
 */
public class DownloadClient {
DefaultHttpClient httpClient;
HttpGet httpGet;
HttpResponse httpResponse;
HttpEntity httpEntity;
InputStream httpInputStream;

private static String LOG_TAG = DownloadClient.class.getName();

public DownloadClient(){
    httpClient = new DefaultHttpClient();
    httpGet = new HttpGet("http://cachefly.cachefly.net/100mb.test");
    for (Header header : httpGet.getAllHeaders()){
        Log.d(LOG_TAG, "--> Header: " + header);
    }

    Log.d(LOG_TAG, "--> Header size is: " + httpGet.getAllHeaders().length);
    try {
        httpResponse = httpClient.execute(httpGet);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    httpEntity = httpResponse.getEntity();
    try {
        httpInputStream = httpEntity.getContent();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(LOG_TAG, "--> StatusLine: " + httpResponse.getStatusLine());
}

public void read(){
    byte[] readBuffer = new byte[32];
    try {
        while (httpInputStream.read(readBuffer) != -1){
            try{Thread.sleep(100);}catch(Exception e){}
            //Log.d(LOG_TAG,"--> Read Bytes: " + new String(readBuffer));
        };
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void shutdown(){
    httpGet.abort();
    httpClient.getConnectionManager().shutdown();
}

}

Kevin

like image 733
Kevin Parker Avatar asked Sep 09 '11 13:09

Kevin Parker


People also ask

Is Apache HttpClient deprecated?

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.

What is content range in HTTP header?

The Content-Range response HTTP header indicates where in a full body message a partial message belongs.

What is byte range request?

Byte-range requests occur when a client asks the server for only a portion of the requested file. The purpose of this is essentially to conserve bandwidth usage by avoiding the need to download a complete file when all that is required is a small section.


1 Answers

I was able to get this working by adding the following line:

httpGet.addHeader("Range", "bytes=0-0");
like image 183
Kevin Parker Avatar answered Oct 02 '22 10:10

Kevin Parker