我需要使用 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/