title: How to download file in Android over HTTPs
body: <p>I am downloading a file in android which goes via plain HTTP. Now i want this connection to go over HTTPs and then download file, can someone help me in code what changes will i need to do that. </p>
  
  <p>I changed </p>
  
  <pre><code>URLConnection ucon = url.openConnection();
  </code></pre>
  
  <p>to </p>
  
  <pre><code>//HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
  </code></pre>
  
  <p>but that didn't work. </p>
  
  <p>Code:</p>
  
  <pre><code>private void DownloadFile() {
  
              try {
                  File root = android.os.Environment
                          .getExternalStorageDirectory();
  
                  File dir = new File(root.getAbsolutePath() + "/File");
                  if (dir.exists() == false) {
                      dir.mkdirs();
                  }
  
                  URL url = new URL(DownloadFile); // you can write here any link
                  File file = new File(dir, fileName);
  
                  long startTime = System.currentTimeMillis();
                  Log.d(LOG_TAG, "download begining");
                  Log.d(LOG_TAG, "download url:" + url);
                  Log.d(LOG_TAG, "downloaded file name:" + fileName);
  
                  /* Open a connection to that URL. */
                  URLConnection ucon = url.openConnection();
                  //HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
  
                  /*
                   * Define InputStreams to read from the URLConnection.
                   */
                  InputStream is = ucon.getInputStream();
                  BufferedInputStream bis = new BufferedInputStream(is);
  
                  /*
                   * Read bytes to the Buffer until there is nothing more to
                   * read(-1).
                   */
                  ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                  int current = 0;
                  while ((current = bis.read()) != -1) {
                      baf.append((byte) current);
                  }
  
                  /* Convert the Bytes read to a String. */
                  FileOutputStream fos = new FileOutputStream(file);
                  fos.write(baf.toByteArray());
                  fos.flush();
                  fos.close();
                  Log.d(LOG_TAG,
                          "download ready in"
                                  + ((System.currentTimeMillis() - startTime) / 1000)
                                  + " sec");
  
              } catch (IOException e) {
                  Log.d(LOG_TAG, "Error: " + e);
              }
  
          }
  </code></pre>