【问题标题】:how to download file over ssl (https) on android如何在 android 上通过 ssl (https) 下载文件
【发布时间】:2013-07-31 15:36:13
【问题描述】:

我有一个应用程序正在使用下面的 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,它会很好地加载它......我只是不知道如何在我的应用程序中执行此操作。

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

谢谢

【问题讨论】:

  • 如果您指定“不起作用”的含义,这个问题会更有帮助。您看到了什么错误消息或其他不希望出现的结果?

标签: android http https download


【解决方案1】:

看看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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多