2013-07-31 71 views
3

我有一个应用程序正在使用下面的代码来处理http,但出于安全原因,这被改为https,但是这会导致下载失败。我试图将httpURLConnection更改为httpsURLConnection,但这并不奏效。如何通过ssl(https)在android上下载文件

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手机和类型上加载它罚款...不需要特别的密码或任何东西我只是无法弄清楚如何在我的应用程序中做到这一点。

我几乎没有安全或证书或任何这方面的经验,所以我甚至不知道这里或在哪里需要什么。

感谢

+0

问题如果你指定了“没有工作”的含义,会更有帮助。你看到了什么错误信息或其他不良结果? – LarsH

回答

3

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