I am sending a request to a server to get some data, but before getting the data, I want to check whether the URL is on http
or https
.
How can I tell that, i.e. how can I check whether the URL is http
or https
?
I have a used a method in which I send a request to a server using http://
. If a protocol exception occurs, then I again send a request to the server with https://
. I can check it this way, but I don't want an exception to occur. Is there any way to check it again?
Follwoing is the code sample to send the request to server: Using OkHttp-
try
{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url[0])
.build();
Response httpResponse = client.newCall(request).execute();
}
catch(IOException e)
{
Log.i("log","exception");
}
Using HtppUrlConnection:
try
{
HttpURLConnection.setFollowRedirects(true);
con = (HttpURLConnection) new URL(url[0]).openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(10000);
}
catch(IOException e)
{
Log.i("log","exception");
}