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


Java URL getAuthority()用法及代碼示例


getAuthority()函數是URL類的一部分。函數getAuthority()返回指定URL的權限。 URL的授權部分是URL的主機名和端口。

函數簽名

public String getAuthority()

用法


url.getAuthority()

參數:此函數不需要任何參數

返回類型:該函數返回字符串類型

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

範例1:

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

範例2:getAuthority()和getHost()函數之間的區別在於,getAuthority()隨端口一起返回主機,而getHost()僅返回主機名。

// Java program to show the 
// use of the function getAuthority() 
  
import java.net.*; 
  
class Solution { 
    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 Authority 
            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


相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 URL getAuthority() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。