My problem
In my android application I get url input from user, like "www.google.com".
I want to find out for the given url whether to use http
or https
.
What I have tried
after referring to some Stack Overflow questions I tried with getScheme()
try {
String url_name="www.google.com";
URI MyUri = new URI(url_name);
String http_or_https="";
http_or_https=MyUri.getScheme();
url_name=http_or_https+"://"+urlname;
Log.d("URLNAME",url_name);
}
catch (Exception e) {
e.printStackTrace();
}
But my above code throws an exception.
My question
Is above approach getScheme() correct or not?
If above approach is incorrect, how to find url http or https?
Try send an HTTP request on the standard port and possibly common alternative ports; e.g. port 8080. If you get a valid response, you have identified a HTTP server. Try the same with an HTTPS request. If the domain name doesn't start with "www." try prefixing it with that.
Simply put, a domain name (or just "domain") is the name of a website. It's what comes after "@" in an email address, or after "www." in a web address. If someone asks how to find you online, what you tell them is usually your domain name.
assertEquals(HttpURLConnection. HTTP_OK, responseCode); When the resource is not found at the URL, we get a 404 response code: URL url = new URL("http://www.example.com/xyz"); HttpURLConnection huc = (HttpURLConnection) url.
A domain name is a domain name, it has nothing to do with protocol. The domain is the WHERE, the protocol is the HOW.
The domain is the location you want to go, the protocol is how do you go there, by bus, by plane, by train or by boat. It makes no sense to ask 'I want to go to the store, how do I ask the store if I should go by train or by car?'
The reason this works in browsers is that the browser usually tries to connect using both http and https if no protocol is supplied.
If you know the URL, do this:
public void decideProtocol(URL url) throws IOException {
if ("https".equals(url.getProtocol())) {
// It is https
} else if ("http".equals(url.getProtocol())) {
// It is http
}
}
You can check, the given url is http or https by using URLUtils.
URLUtil.isHttpUrl(String url); returns True if the url is an http.
URLUtil.isHttpsUrl(String url); returns True if the url is an https.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With