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.
The string "example.com" is not a URL. It doesn't have a protocol.
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.
You can't assume that there is any website associated with a domain.
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.
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.
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.
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) → Network → Protocol.
It will tell you the protocol used and the domain of each transfer.
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
Regular Expression to Validate Username
Spring - Read Property Value from Properties File in Static Field of Class
How to Access Variable Outside a Try Catch Block
Android Room Persistent: Appdatabase_Impl Does Not Exist
Spring Boot Controller Not Mapping
How to Mock System.Getproperty Using Mockito
Efficient Way of Processing Large CSV File Using Java
Random Number Generation Without Repetition in Java
How to Initialize a Byte Array in Java
Notifydatasetchanged() Makes the List Refresh and Scroll Jumps Back to the Top
Gson Expected Begin_Array But Was String At Line 1 Column 62
How to Return a List of Specific Type Instead of List<Object[]> in Hibernate
How to Junit Test That Two List<E> Contain the Same Elements in the Same Order
How to Use Limit in Spring Within SQL Query
How to Pass Variables Between Cucumber-Jvm Steps
At Runtime, Find All Classes in a Java Application That Extend a Base Class