Check if URL is HTTPS or HTTP protocol?

25,102

Solution 1

I think this works, at least it seems to be working, what do you guys think? I place this if statement just before the httpsYes = True and httpYes = True.

It seems that when the HTTPS protocol is selected it wants to redirect using response code 302, but for all other instances it connects with response code 200. I throw a new ConnectionException() error as that takes the user back to the settings screen to correct the URL error.

For the HTTPS protocol:

if (httpsURLConnection.getResponseCode() != 200) {
     throw new ConnectException();
}

For the HTTP protocol:

if (urlConnection.getResponseCode() != 200) {
     throw new ConnectException();
}

Comments? Should I use urlConnection.getResponseCode() > 199 && < 300? To cover all successful connects?

Solution 2

You can Use android URLUtil to check whether url is HTTP or HTTPS:

public static boolean isHttpUrl (String url)
Returns True iff the url is an http: url.

public static boolean isHttpsUrl (String url) 
Returns True iff the url is an https: url.

Edit:

public static boolean isValidUrl (String url)
Returns True iff the url is valid.

Solution 3

URLConnection result = url.openConnection();
if (result instanceof HttpsURLConnection) {
   // https
}
else if (result instanceof HttpURLConnection) {
   // http
}
else {
  // null or something bad happened
}

Solution 4

Try this code:

mConnexion = (URLUtil.isHttpsUrl(mStringUrl)) ? (HttpsURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection();

Solution 5

I have checked URLUtil class and checked that its contain so many methods for these kind of stuff, but i just extend the answer that you can simply do as below also :-

public static boolean isHttpOrHttpsUrl(String url) {
        String patter = "^(http|https|ftp)://.*$";
        if (url.matches(patter)){
            return true;
        }else{
            return false;
        }
    }
Share:
25,102
Dino
Author by

Dino

Updated on July 10, 2022

Comments

  • Dino
    Dino almost 2 years

    I am currently using the following to read a file from android docs here and here. 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 HttpURLConnection and HttpsURLConnection, but if their site uses HTTPS protocol then it doesn't work for HttpURLConnection protocol and worst of all it doesn't give me an exception error. Below is the sample code that I am using.

    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?

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

    EDIT:

    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?

    How can I check if the website uses HTTPS or HTTP protocol? If the user then only puts in say www.google.com and I append https:// or http:// prefix to it, how do I know which one is the correct one to use?

  • Dhaval Patel
    Dhaval Patel almost 9 years
    @ Kartheek is it different from my answer?
  • Kartheek
    Kartheek almost 9 years
    I didn't checked your's answer @DhavalPatel
  • Dhaval Patel
    Dhaval Patel almost 9 years
    @ Tony Why to add extras overhead of try-catch? If we can check it by simple if- else condition?
  • Antwan
    Antwan almost 9 years
    i'm 100% with you but it could be added as an extra scenario. and by the way the try catch statement as added in both cases for http connection
  • Dino
    Dino almost 9 years
    I have tried with all of the above. Even though it is correct and works, it doesn't solve the problem if the user uses http instead of https, it still returns a valid response.
  • Bojan Kseneman
    Bojan Kseneman almost 9 years
    Does this actually try to transfer any data or it just returns true if the connections starts with https://
  • Dhaval Patel
    Dhaval Patel almost 9 years
    @Dino Not sure but may be your HTTP URL is redirecting to HTTPS.
  • Dino
    Dino almost 9 years
    @DhavalPatel I am not sure if it is trying to redirect, but I don't get an exeption error if the user types in the incorrect prefix. It returns true. I have tried it on both HTTP and HTTPS prefix.
  • Dino
    Dino almost 9 years
    @DhavalPatel, I just used getResponseCode() and it does return 302 (which means re-direct) for HTTPS
  • Mário Feroldi
    Mário Feroldi over 6 years
    It doesn't handle the case where you don't have the protocol specified in the URL.
  • Elie G.
    Elie G. over 5 years
    Anyway with this regex it'll cast the object to https even if it's not