Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check given domain name http or https in java?

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?

like image 866
Ranjithkumar Avatar asked Apr 13 '15 04:04

Ranjithkumar


People also ask

How do I know if my domain is 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.

How do I find the URL of my domain name?

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.

How do you check if a URL exists or returns 404 with Java?

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.


2 Answers

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
            }
    }
like image 109
DKIT Avatar answered Oct 20 '22 03:10

DKIT


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.

like image 29
Krishna V Avatar answered Oct 20 '22 05:10

Krishna V