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?
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?
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.
This can be verified using Util .
- isHttpUrl returns True if url is url: url.
- isHttpsUrl returns True if the url is https: url.
URLConnection result = url.openConnection();
if (result instanceof HttpsURLConnection) {
// https
}
else if (result instanceof HttpURLConnection) {
// http
}
else {
// null or something bad happened
}
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;
}
My recommendation is created by function regex. eg:
public void testUrl(Object urlHttp){
String url = "https://www.google.com";
if(url.matches("^(https?)://.*$")){
Object o = (HttpsURLConnection) urlHttp;
}else{
Object o = (HttpURLConnection) urlHttp;
}
}
Hello
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;
}
}
Try this code:
mConnexion = (URLUtil.isHttpsUrl(mStringUrl)) ? (HttpsURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection();