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

后端 未结 7 935
说谎
说谎 2021-01-11 10:48

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

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 11:23

    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
                }
        }
    

提交回复
热议问题