I need to write a utility method which will take a url and check whether given url is valid or not ?
URL can be anything with/without protocol like http, https or with can contain the relative url like if domain is example
and url is "abc.com" then its a relative URL. also it can be invalid url as well and can contain simple string.
Also i've list of whitelisted URL and can change runtime like youtube.com
, facebook.com
etc.
How can i check if given url is valid in my case, some basic check which i am doing is below :-
String url = "http://youtube.com";
if(!StringUtil.isEmpty(url))
{
if (url.startsWith("http:") || pathToImage.startsWith("https://")) {
// check if url is from whitlist domains
} else {
// do nothing, url is not internal domain.
}
}
Now my question is how do i properly extract the domain name from the URL which will be after the http
or https
.
Note:- I am using apache StringUtils and its quite possible that url can be like https://absdsbsb
or https://anmds.txt
. Also let me know if its a good case for regex matching ?