Java URL to Host Name getHostname(String urlStr)

Here you can find the source of getHostname(String urlStr)

Description

returns the hostname of the given URL

License

Apache License

Parameter

Parameter Description
urlStr URL

Return

hostname; null if the url is malformed

Declaration

public static String getHostname(String urlStr) 

Method Source Code

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

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**/*from  w ww  .j a v  a2s. c o  m*/
     * returns the hostname of the given URL
     * 
     * @param urlStr
     *            URL
     * @return hostname; null if the url is malformed
     */
    public static String getHostname(String urlStr) {
        URL url;
        try {
            url = new URL(urlStr);
            String hostname = url.getHost();
            return hostname;
        } catch (MalformedURLException e) {
            return null;
        }

    }
}

Related

  1. getHostname(String completeUrl)
  2. getHostName(String url)
  3. getHostName(String url)
  4. getHostName(String url)
  5. getHostName(String url)
  6. getHostSegments(URL url)