Java Utililty Methods URL to Host Name

List of utility methods to do URL to Host Name

Description

The list of methods to do URL to Host Name are organized into topic(s).

Method

StringgetHost(String url)
Returns the hostname or ip address portion of a url.
try {
    URL u = new URL(url);
    return u.getHost();
} catch (Exception e) {
return url;
StringgetHost(String url)
Returns the lowercased hostname for the url or null if the url is not well formed.
try {
    return new URL(url).getHost().toLowerCase();
} catch (MalformedURLException e) {
    return null;
StringgetHost(String url)
Parses URL and get host from it, if it is possible
if (url.contains("://")) {
    URI uri = URI.create(url);
    return uri.getHost();
} else if (GIT_URL_PATTERN.matcher(url).matches()) {
    return url.substring(url.indexOf("@") + 1, url.indexOf(":"));
} else {
    return null;
StringgetHost(String url)
get Host
URI uri = null;
try {
    uri = new URI(url);
} catch (URISyntaxException e) {
    System.out.println("Omg, It's not a valid URL: " + url);
    return "";
String host = uri.getHost();
...
StringgetHost(String url)
get Host
if (isEmpty(url)) {
    return url;
String host = null;
if (url.contains(SCHEME_SEPARATOR)) {
    try {
        host = new URI(url).getHost();
    } catch (URISyntaxException ignored) {
...
StringgetHost(String urlString)
get Host
try {
    URL url = new URL(urlString);
    return url.getHost();
} catch (MalformedURLException e) {
    throw e;
StringgetHost(String urlString)
Retrieves the host from a URL {Category} StringUtil {param} string(url) url: String.
if (urlString == null) {
    return null;
urlString = urlString.trim().toLowerCase();
if (urlString.startsWith("http://") == false) {
    urlString = "http://" + urlString;
String decUrl = null;
...
StringgetHost(URL url)
get Host
return url.getHost() + ":" + (url.getProtocol().equals("http") ? 80 : url.getPort());
byte[]getHostAddressAsBytes(String url)
Returns the Host Address of a given URL as byte[].
return InetAddress.getByName(getHostAddress(url)).getAddress();
StringgetHostAndPort(final URL url)
get Host And Port
return url.getHost() + ":" + getSanitizedPort(url);