title: Check if URL is HTTPS or HTTP protocol?
body: <p>I am currently using the following to read a file from android docs <a href="http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html" rel="noreferrer">here</a> and <a href="http://developer.android.com/reference/java/net/HttpURLConnection.html" rel="noreferrer">here</a>. The user selects (in the settings screen) if their site uses HTTP or HTTPS protocol. If their website uses the HTTP protocol then it works for both <code>HttpURLConnection</code> and <code>HttpsURLConnection</code>, but if their site uses HTTPS protocol then it doesn't work for <code>HttpURLConnection</code> protocol and worst of all it doesn't give me an exception error. Below is the sample code that I am using. </p>
  
  <p>So in essence, how can I check to see if the web url is HTTPS protocol so checking if the user selected the correct protocol?  </p>
  
  <pre><code>InputStream inputStream;
  HttpURLConnection urlConnection;
  HttpsURLConnection urlHttpsConnection;
  boolean httpYes, httpsYes; 
  try {
  if (httpSelection.equals("http://")) {
    URL url = new URL(weburi);
    urlConnection = (HttpURLConnection) url.openConnection();
    inputStream = new BufferedInputStream((urlConnection.getInputStream()));
  httpYes = True;
  }
  if (httpSelection.equals("https://")) {
    URL url = new URL(weburi);
    urlHttpsConnection = (HttpsURLConnection) url.openConnection();
    urlHttpsConnection.setSSLSocketFactory(context.getSocketFactory());
    inputStream = urlHttpsConnection.getInputStream();
    https=True;
  }
  
  catch (Exception e) {
  //Toast Message displays and settings intent re-starts
  }
  finally {
    readFile(in);
  if(httpYes){ 
      urlConnection.disconnect();
      httpYes = False;
   }
  if(httpsYes){ 
      urlHttpsConnection.disconnect();
      httpsYes = False;
   } 
  }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>To elaborate some more. I need to see if it returns a valid response from a website? So if the user selected http instead of https how can I check to see if http is the incorrect prefix/protocol?</p>
  
  <p>How can I check if the website uses HTTPS or HTTP protocol? If the user then only puts in say <strong>www.google.com</strong> and I append <strong>https://</strong> or <strong>http://</strong> prefix to it, how do I know which one is the correct one to use?</p>