Java URL to Host Name getHostName(String url)

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

Description

get Host Name

License

Open Source License

Declaration

public static String getHostName(String url) throws MalformedURLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.MalformedURLException;

public class Main {
    public static String getHostName(String url) throws MalformedURLException {
        if (url == null || url.length() == 0)
            return "";
        url = url.toLowerCase();/*from  ww w . j a v  a2 s  . c  om*/
        if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("ftp://"))
            throw new MalformedURLException("Unknown schema!");

        int doubleslash = url.indexOf("//");
        doubleslash += 2;

        int end = url.length();

        int idx = url.indexOf('/', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf(':', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('&', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('=', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('#', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        idx = url.indexOf('?', doubleslash);
        if (idx > 0 && idx < end)
            end = idx;

        return url.substring(doubleslash, end);
    }
}

Related

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