HTTPS 安卓下载管理器

4

I require to download files using HTTPS, but it seems that standard DownloadManager doesn't support HTTPS, only Http.

I've made some resarch and found just one topic about it, but it doesn't help me too much. alvinsj from that topic(https://github.com/alvinsj/android-https-downloadmanager-demo) suggesed a solution where he just amended the check in source code:

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

It looks not correct to me because even though it would work it will not be sequre. I really need your help! Some examples, thoughts will be much appreciated!

Thanks


我正在使用HTTP和HTTPS,所以我建议你参考这个链接:http://www.codeproject.com/Articles/396027/Integrating-HTTP-and-HTTPS-Connection。希望对你有所帮助。 - UchihaSasuke
2个回答

3

我找到了答案——非常简单。我们调整了服务器以使用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();
}
}

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接