如何下载文件通过SSL(HTTPS)上的android [英] how to download file over ssl (https) on android

查看:285
本文介绍了如何下载文件通过SSL(HTTPS)上的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序正在使用下面的HTTP的code,但是这正在改变到https安全方面的原因,但是这会导致下载失败。我试着只是改变了的HttpURLConnection httpsURLConnection 然而,这没有奏效。

I have an app that is working using the code below for http, however for security reasons this is being changed to https, but this causes the download to fail. I tried just changing the httpURLConnection to httpsURLConnection however this did not work.

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");
    }

有从我的电脑,事实上连接,如果我去到浏览器在Android手机和类型有问题的HTTPS URL加载它罚款...我只是不能计算出如何无需特殊密码或任何东西要做到这一点在我的应用程序。

there are no particular passwords or anything needed to connect from my computer and in fact if I go to the browser on the android phone and type in the HTTPS URL in question it loads it fine... I just cant figure out how to do it in my app.

我几乎没有任何安全或证书或者类似的,所以我甚至不知道这里需要什么,或去哪里找经验。

I have virtually no experience with security or certificates or any of that so I am not even sure what is needed here or where to look.

感谢

推荐答案

有一个看的 HTTPS和Android文档内SSL文章。他们在那里有一个简单的例子,以你的HTTPS证书由可信CA签名(因为你写的,你是可以使用的服务器与浏览器,你有这样的签名证书):

Have a look at the HTTPS and SSL Article within the Android documentation. They have an simple example in there, given your HTTPS certificate is signed by a trusted CA (as you write that you're able to use the server with a browser you have such a signed certificate):

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

这篇关于如何下载文件通过SSL(HTTPS)上的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆