【问题标题】:Is it possible to submit cookies in an Android DownloadManager是否可以在 Android DownloadManager 中提交 cookie
【发布时间】:2012-06-12 00:05:45
【问题描述】:

我正在使用 android DownloadManager API 从我学校的服务器下载文件。我有权通过登录访问这些文件,但我无法弄清楚如何使用我的DownloadManager.Request 提交 cookie 下载代码如下。 dm 是一个全局的DownloadManagerurl 是一个 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,但我在文档中看不到任何这样做的方法。

【问题讨论】:

    标签: android cookies download


    【解决方案1】:

    Cookie 是通过 HTTP 标头(命名为“Cookie”)发送的,幸运的是,DownloadManager.Request has a method 可以添加您自己的标头。

    所以你想做的是这样的:

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

    当然,您必须将“内容”替换为实际的 cookie 内容。 CookieManager 类对于获取站点的当前 cookie 应该很有用,但如果失败,另一种选择是让您的应用程序发出登录请求并获取返回的 cookie。

    【讨论】:

    • 效果很好。我已经在 Map<String, String> 中手动管理 cookie,所以它就像格式化 KEY1=VALUE1;KEY2=VALUE2; 并将其传递给您提到的函数一样简单。谢谢!
    • 这简直太完美了——已经走过了其他几条路,但这次成功了!我通过调用“CookieManager.getInstance().getCookie(url);”从 onPageFinished 获取我的 cookie;然后我可以直接将结果用作 Cookie 值!
    【解决方案2】:

    您可以使用 CookieManager 检索 cookie,如下所示(我使用 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);
    

    然后将其传递给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.
    

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 2016-05-20
      • 2011-03-26
      • 2013-05-01
      • 2013-12-27
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多