android - https 安卓下载管理器

标签 android https android-download-manager

我需要使用 HTTPS 下载文件,但标准 DownloadManager 似乎不支持 HTTPS,仅支持 Http。

我做了一些研究,只找到了一个关于它的话题,但它对我没有太大帮助。 来自该主题(https://github.com/alvinsj/android-https-downloadmanager-demo)的 alvinsj 提出了一个解决方案,他刚刚修改了源代码中的 checkin :

if (scheme == null || !(scheme.equals("http") ||scheme.equals("https"))) {
            throw new IllegalArgumentException("Can only download HTTP URIs: " + uri);
}

它对我来说看起来不正确,因为即使它可以工作,它也不会是 sequre。 我真的需要你的帮助!一些例子,想法将不胜感激!

谢谢

最佳答案

我找到了答案 - 这很简单。我们将服务器调整为使用 Base64 身份验证,因此我只需为我的请求设置一个 requestHeader:

DownloadManager.Request request = new DownloadManager.Request(downloadUri);

StringBuilder header = new StringBuilder().append("Basic ");
Pair<String, String> serverCredentials = getServerCredentials();
if (!serverCredentials.first.isEmpty()) {
try {
    header.append(EncryptionUtils.toBase64fromString(new StringBuilder().append(serverCredentials.first)
        .append(":").append(serverCredentials.second).toString()));
    request.addRequestHeader("Authorization", header.toString());
} catch (Exception e) {
    e.printStackTrace();
}
}

关于android - https 安卓下载管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197573/

相关文章:

java - 单击按钮尝试插入 SQLite 后崩溃(代码和堆栈跟踪)

security - 在不安全的情况下公开安全 Web 应用程序的静态资源有哪些危险?

Arduino MKGSM 中的 HTTP POST 到 Eventhub

android - 使用 Android Pie 时下载失败并出现错误 400

android - 如何生成用于模拟的 android.jar?

javascript - React-Native 发送html格式邮件

javascript - nodejs https错误: socket hang up with localhost

java - 如何从服务器下载视频文件?

android - 防止媒体扫描仪扫描在 Android 上使用 DownloadManager 下载的 JPG? (开发商)

android - show 为 android 中的后退按钮路径指定条件