Java URL to Host Name getHostName(String url)

Here you can find the source of getHostName(String url)

Description

get Host Name

License

Apache License

Declaration

public static String getHostName(String url) throws URISyntaxException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static String getHostName(String url) throws URISyntaxException {
        URI uri = new URI(url);
        String hostname = uri.getHost();
        // to provide fault proof result, check if not null then return only hostname, without www.
        if (hostname != null) {
            hostname = hostname.startsWith("www.") ? hostname.substring(4) : hostname;
            hostname = uri.getScheme() + "://" + hostname;
            return hostname;
        }/*from ww w .j  a v a 2  s  .c om*/
        return hostname;
    }
}

Related

  1. getHostAddressAsBytes(String url)
  2. getHostAndPort(final URL url)
  3. getHostAndPortFromUrl(String url)
  4. getHostFromURL(final String urlSpec)
  5. getHostname(String completeUrl)
  6. getHostName(String url)
  7. getHostName(String url)
  8. getHostName(String url)
  9. getHostname(String urlStr)