how to download file over ssl (https) on android

android, download, http, https

Solution

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

Problem

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

Original source