Check if the URL is HTTPS or HTTP protocol?
I am currently using the following to read a file from android docs here and. The user selects (on the settings screen) if their site uses the HTTP or HTTPS protocol. If their website uses HTTP protocol it works for HttpURLConnection
both HttpsURLConnection
, but if their website uses HTTPS protocol then it doesn't work for protocol HttpURLConnection
and worst of all it doesn't give me an error exception. Below is an example of the code I am using.
So in essence, how can I check if a web url is HTTPS by checking if the user has 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:
Come up with something else. I need to know if it is returning a valid response from the website? So, if the user chose http over https, how can I check if http is the wrong prefix / protocol?
How to check if a site is using HTTPS or HTTP? If the user then puts www.google.com in the text and prefixes it with https: // or http: // , how do I know which one is correct?
source to share
I think it works, at least it looks like you are working, what do you guys think? I put this if statement before httpsYes = True
and httpYes = True
.
It looks like when HTTPS is selected it wants to redirect using a 302 response code, but for all other instances it connects with a 200 response code. I'm throwing a new error ConnectionException()
as it takes the user back to settings to fix the URL error.
For 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 connections?
source to share
You can use android URLUtil to check if the 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.
source to share
you can try this
boolean httpYes, httpsYes;
try {
URL url = new URL(weburi);
urlConnection = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream((urlConnection.getInputStream()));
httpYes = True;
}
catch (Exception e) {
//Toast Message displays and settings intent re-starts
URL url = new URL(weburi);
urlHttpsConnection = (HttpsURLConnection) url.openConnection();
urlHttpsConnection.setSSLSocketFactory(context.getSocketFactory());
inputStream = urlHttpsConnection.getInputStream();
https=True;
}
source to share
I checked the URLUtil class and verified that it contains so many methods for this kind of thing, but I'm just extending the answer that you can just do it like below: -
public static boolean isHttpOrHttpsUrl(String url) {
String patter = "^(http|https|ftp)://.*$";
if (url.matches(patter)){
return true;
}else{
return false;
}
}
source to share