Java URL to Host Name getHostname(String completeUrl)

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

Description

This method will parse the passed in String which is presumably a complete URL and return the base URL e.g.

License

Open Source License

Parameter

Parameter Description
vDCUrl the VDC Href to parse

Return

the base url of the vCloud

Declaration

public static String getHostname(String completeUrl) 

Method Source Code

//package com.java2s;
/*/*from  www  .j  a  va  2 s. c  om*/
 * Copyright (c) 2013 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.    You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * This method will parse the passed in String which is presumably a complete URL and return the
     * base URL e.g. https://vchs.vmware.api/ from the component parts of the passed in URL.
     * 
     * @param vDCUrl
     *            the VDC Href to parse
     * @return the base url of the vCloud
     */
    public static String getHostname(String completeUrl) {
        URL baseUrl = null;

        try {
            // First create a URL object
            baseUrl = new URL(completeUrl);
            // Now use the URL implementation to provide the component parts, leaving out
            // some parts so that we can build just the base URL without the path and query string
            return new URL(baseUrl.getProtocol(), baseUrl.getHost(), baseUrl.getPort(), "").toString();
        } catch (MalformedURLException e) {
            throw new RuntimeException("Invalid URL: " + completeUrl);
        }
    }
}

Related

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