Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager with cookie authentication

I'm trying to get a zip file using DownloadManager parsing a cookie with a JSESSIONID from my server. I'm getting this JSESSIONID doing all my process login using HTTPCLIENT lib and setting a variable JSESSIONID for later use in my DownloadManager request.

My download request:

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(htmlUrlDownload));

    request.addRequestHeader("Cookie", "JSESSIONID=" + JSESSIONID);
    request.addRequestHeader(Constants.USER_AGENT, Constants.TARGET_REQUEST_HEADER);

    request.setDescription("Baixando " + metaDado.getType());
    request.setTitle("Download");
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    String nameFile = offlineUuid + ".zip";

    fileName = nameFile;

    filePath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DOWNLOADS
            + File.separator + fileName;

    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameFile);

    final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    final long downloadId = manager.enqueue(request);

The problem is that download never starts and the COLUMN_REASON returns a code to ERROR_UNHANDLED_HTTP_CODE

But if I try to download the same file using a dropbox link without authentication or using httpclient, it's works perfectly, what am I doing wrong?

How can I get a better msg error?

like image 929
Victor Laerte Avatar asked Apr 05 '13 22:04

Victor Laerte


1 Answers

Nothing wrong in your code, seems like your server is responding with HTTP redirect staus code, which is causing DownloadManager to fail to process download.

From Android Docs :-

public static final int ERROR_UNHANDLED_HTTP_CODE

Added in API level 9 Value of COLUMN_REASON when an HTTP code was received that download manager can't handle.

See below code snippet:-

From Android Framework source code DownloadManger.java

case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
                case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
                    return ERROR_UNHANDLED_HTTP_CODE;

So you need to check your Server logs for this issue, or pass direct file url to download manager which doesn't cause any redirect.

Also please note that you need below two permissions in your Androidmanifest.xml for download to work as expected

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
like image 110
Akhil Avatar answered Oct 08 '22 23:10

Akhil