當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。