Is it possible to submit cookies in an Android DownloadManager

Cookies are sent via an HTTP header (named, appropriately enough, "Cookie"), and fortunately, DownloadManager.Request has a method to add your own headers.

So what you'd want to do is something like this:

Request request = new Request(Uri.parse(url)); 
request.addRequestHeader("Cookie", "contents");
dm.enqueue(request);

You'll have to replace "contents" with the actual cookie contents, of course. The CookieManager class should be useful to get the current cookie for the site, but if that fails, another option would be to have your application make a login request and grab the returned cookie.


You can retrieve cookies with CookieManager like below (where I used webview):

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            String cookieString = CookieManager.getInstance().getCookie(url);

        }
    });

    //Enable Javascript
    webView.getSettings().setJavaScriptEnabled(true);
    //Clear All and load url
    webView.loadUrl(URL_TO_SERVE);

And then pass it to DownloadManager:

  //Create a DownloadManager.Request with all the information necessary to start the download
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
            .setTitle("File")// Title of the Download Notification
            .setDescription("Downloading")// Description of the Download Notification
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
            .setDestinationUri(Uri.fromFile(file))// Uri of the destination file
            .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
            .setAllowedOverRoaming(true);
    request.addRequestHeader("cookie", cookieString);
    /*request.addRequestHeader("User-Agent", cookieString);*/
    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.