Check If Url Is Https or Http Protocol

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

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

How to identify if domain is http or https?

There are a number of misconceptions here.

  1. The string "example.com" is not a URL. It doesn't have a protocol.

  2. A domain is just that. A domain. It is not a URL. It is not a website. It is just a sequence of characters that the DNS can resolve to ... something.

  3. You can't assume that there is any website associated with a domain.

  4. The "www." convention is redundant, old fashioned and ... (IMO) ... ugly. Most places who use "www.example.com" will also have a server at "example.com", and one will redirect to the other as appropriate.

  5. You can't always assume that the server is on the standard port (80 or 443). It is even possible that an https server uses port 80 or an http server uses 443. (Crazy ... but possible)

So how do you figure out if a given domain name has an associated website, what the website's URL is, and whether it is "http:" or "https:"?

Trial and error. There is no other way.

  1. 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.

  2. Try the same with an HTTPS request.

  3. If the domain name doesn't start with "www." try prefixing it with that.

But resign yourself to the possibility that none of the above work.

How to get the protocol (http or https) of the website using Python

It works for stackoverflow because when you first visit stackoverflow.com on port 80 (the http port), stackoverflow's servers notify the browser that the link has been permanently moved to https.

To detect the same in Python, use the requests library, like this:

>>> import requests
>>> r = requests.get('http://stackoverflow.com') # first we try http
>>> r.url # check the actual URL for the site
'https://stackoverflow.com/'

To find out how the URL changed, look at the history object, and you will see a 301 response, which means the URI has moved permanently to a new address.

>>> r.history[0]
<Response [301]>
>>> r.history[0].url # this is the original URL we tried
'http://stackoverflow.com/'

How to check if a website has HTTP/2 protocol support

You can just check it in: Chrome Dev Tool (F12) → NetworkProtocol.

It will tell you the protocol used and the domain of each transfer.

Chrome Dev Tool (F12) -> Network -> Protocol

Legend

http/1.1 = HTTP/1.1

h2          = HTTP/2


Note: If you cannot see the Protocol column, just right-click on any header and check the "Protocol" label.



Related Topics



Leave a reply



Submit