gpt4 book ai didi

android - 如何在安卓上通过 ssl (https) 下载文件

转载 作者:太空狗 更新时间:2023-10-29 12:46:08 25 4
gpt4 key购买 nike

我有一个应用程序正在使用下面的 http 代码,但出于安全原因,它被更改为 https,但这会导致下载失败。我尝试将 httpURLConnection 更改为 httpsURLConnection 但这不起作用。

try {

FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
Log.d("downloader","downloading");
}

f.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("downloader", "catch");
}

没有特定的密码或任何需要从我的电脑连接的东西,事实上,如果我去安卓手机上的浏览器并输入有问题的 HTTPS URL,它加载它很好......我只是不知道如何在我的应用程序中执行此操作。

我几乎没有安全或证书等方面的经验,所以我什至不确定这里需要什么或在哪里寻找。

谢谢

最佳答案

看看 HTTPS and SSL Article within the Android documentation .他们在那里有一个简单的例子,假设您的 HTTPS 证书是由受信任的 CA 签名的(正如您所写的那样,您可以使用具有此类签名证书的浏览器来使用服务器):

URL url = new URL("https://wikipedia.org");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);

关于android - 如何在安卓上通过 ssl (https) 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17974733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com