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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。