当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java URL getHost()用法及代码示例


getHost()函数是URL类的一部分。函数getHost()返回指定URL的主机。 URL的主机部分是URL的主机名。主机的格式符合RFC 2732。

函数签名

public String getHost()

用法


url.getHost()

参数:此函数不需要任何参数

返回类型:该函数返回字符串类型

以下示例程序旨在说明getHost()函数的用法:

范例1:

// Java program to show the use 
// of the function getHost() 
  
import java.net.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
  
            // create a URL 
            url 
                = new URL("https:// www.geeksforgeeks.org"); 
  
            // get the Host 
            String Host = url.getHost(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the Host 
            System.out.println("Host = " + Host); 
        } 
  
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
输出:
URL = https:// www.geeksforgeeks.org
Host =  www.geeksforgeeks.org

范例2:

// Java program to show the 
// use of the function getHost() 
  
import java.net.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
            // create a URL 
            url 
                = new URL( 
                    "https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/"); 
  
            // get the Authority 
            String authority 
                = url.getAuthority(); 
  
            // get the Host 
            String host = url.getHost(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the Host 
            System.out.println("Authority = "
                               + authority); 
  
            // display the Host 
            System.out.println("Host = " + host); 
        } 
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
输出:
URL = https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/
Authority =  www.geeksforgeeks.org:80
Host =  www.geeksforgeeks.org

范例3:如果我们创建没有主机名的URL,并使用getHost()函数请求主机,则该函数将返回空白字符串。

// Java program to show the 
// use of the function getHost() 
  
import java.net.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
        // url  object 
        URL url = null; 
  
        try { 
            // create a URL 
            url = new URL("https://"); 
  
            // get the Host 
            String Host = url.getHost(); 
  
            // display the URL 
            System.out.println("URL = " + url); 
  
            // display the Host 
            System.out.println("Host = " + Host); 
        } 
  
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
输出:
URL = https:
Host =


相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 URL getHost() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。