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


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


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

函數簽名

public String getAuthority()

用法


url.getAuthority()

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

返回類型該函數返回String Type,它是指定URL的權限。

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

範例1:

// Java program to show the 
// use of the function getAuthority() 
  
import java.net.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
        // URI  object 
        URI uri = null; 
  
        try { 
            // create a URI 
            uri 
                = new URI( 
                    "https://www.geeksforgeeks.org"); 
  
            // get the Authority 
            String authority 
                = uri.getAuthority(); 
  
            // display the URI 
            System.out.println("URI = " + uri); 
  
            // display the Authority 
            System.out.println("Authority = "
                               + authority); 
        } 
  
        // if any error occurs 
        catch (Exception e) { 
  
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URI = 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 GFG { 
    public static void main(String args[]) 
    { 
        // url  object 
        URI uri = null; 
  
        try { 
            // create a URI 
            uri 
                = new URI( 
                    "https://www.geeksforgeeks.org:80"); 
  
            // get the Authority 
            String authority 
                = uri.getAuthority(); 
  
            // get the Host 
            String host = uri.getHost(); 
  
            // display the URI 
            System.out.println("URI = " + uri); 
  
            // 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); 
        } 
    } 
}
輸出:
URI = https://www.geeksforgeeks.org:80
Authority = www.geeksforgeeks.org:80
Host = www.geeksforgeeks.org

範例3:getAuthority()和getRawAuthority()返回的值是相同的,隻是對所有轉義八位位組的序列進行了解碼。 getRawAuthority()返回用戶提供的字符串的確切值,但getAuthority()函數解碼轉義八位字節的序列(如果有)。

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

]



相關用法


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