Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to submit cookies in an Android DownloadManager

I'm using the android DownloadManager API to download files from my school's server. I have permission to access these files with a login, but what I haven't been able to figure out is how to submit cookies with my DownloadManager.Request The download code is below. dm is a global DownloadManager, and url is a php download script which redirects to a file, usually pdf/doc/etc.

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(url));
dm.enqueue(request);

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);

This works fine, but I get an html file downloaded, which is the login page of my school's website. Obviously I need to submit the user's session cookies somehow, but I can't see any way of doing this in the documentation.

like image 949
Logiraptor Avatar asked Jun 07 '12 21:06

Logiraptor


People also ask

How do I enable cookies on Android WebView?

How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);

What is cookie Manager Android?

android.webkit.CookieManager. Manages the cookies used by an application's WebView instances. CookieManager represents cookies as strings in the same format as the HTTP Cookie and Set-Cookie header fields (defined in RFC6265bis).

How do I clear cookies on WebView Android?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.


2 Answers

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.

like image 173
Michael Madsen Avatar answered Sep 28 '22 03:09

Michael Madsen


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.
like image 43
Farmaker Avatar answered Sep 28 '22 01:09

Farmaker