是否可以在Android DownloadManager中提交cookie
我正在使用安卓是否可以在Android DownloadManager中提交cookie,android,cookies,download,Android,Cookies,Download,我正在使用安卓DownloadManagerAPI从学校服务器下载文件。我有权限通过登录访问这些文件,但我没有弄清楚如何使用我的DownloadManager提交cookies。请求下载代码如下dm是一个全局DownloadManager,url是一个php下载脚本,它重定向到文件,通常是pdf/doc/等 dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); Request request = new Request(Uri.pa
DownloadManager
API从学校服务器下载文件。我有权限通过登录访问这些文件,但我没有弄清楚如何使用我的DownloadManager提交cookies。请求下载代码如下dm
是一个全局DownloadManager
,url
是一个php下载脚本,它重定向到文件,通常是pdf/doc/等
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,但我在文档中看不到任何方法。Cookie通过HTTP标头(适当地命名为“Cookie”)发送,幸运的是,DownloadManager。请求添加您自己的标头
所以你想做的是这样的:
Request request = new Request(Uri.parse(url));
request.addRequestHeader("Cookie", "contents");
dm.enqueue(request);
当然,您必须将“内容”替换为实际的cookie内容。该类对于获取站点的当前cookie应该很有用,但如果失败,另一个选项是让应用程序发出登录请求并获取返回的cookie。您可以使用CookieManager检索cookie,如下所示(我使用的是webview):
然后将其传递给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.
这非常有效。我已经在Map
中手动管理cookies了,所以它的格式非常简单,就像KEY1=VALUE1;键2=值2
并将其传递给您提到的函数。谢谢这真是太完美了——我们走过其他几条路,但这一次成功了!我通过调用“CookieManager.getInstance().getCookie(url);”从onPageFinished获取Cookie,然后我可以将结果直接用作Cookie值!
//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.