Is it possible to submit cookies in an Android DownloadManager

android, cookies, download

Solution

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.

Problem

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.

Original source