是否有可能在Android下载管理器提交饼干 [英] Is it possible to submit cookies in an Android DownloadManager

查看:163
本文介绍了是否有可能在Android下载管理器提交饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的下载管理器 API下载从我学校的服务器上的文件。我有权限访问这些文件与登录,但我一直无法弄清楚是如何与我的 DownloadManager.Request 下载$ C $提交饼干c是如下。 DM 是一个全球性的下载管理器网​​址是一个PHP其重定向到一个文件,通常PDF / DOC /等。

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);

这工作得很好,但我得到下载一个HTML文件,这是我校网站上的登录页面。很显然,我需要以某种方式提交用户的会话cookie,但我看不到任何方式在文档中这样做的。

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.

推荐答案

Cookies是通过HTTP头中发送(命名,恰如其分,曲奇),幸运的是,DownloadManager.Request <一href="http://developer.android.com/reference/android/app/DownloadManager.Request.html#addRequestHeader%28java.lang.String,%20java.lang.String%29">has方法添加自己的头。

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);

您将必须与实际cookie的内容替换,当然内容。该 CookieManager 类应该是有用的,以获得当前的Cookie的网站,但如果失败,另一种选择是让你的应用程序中所做的登录请求,并抢回来的cookie。

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.

这篇关于是否有可能在Android下载管理器提交饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆