📜  Java中的 URI getAuthority() 方法和示例

📅  最后修改于: 2022-05-13 01:54:26.652000             🧑  作者: Mango

Java中的 URI getAuthority() 方法和示例

getAuthority()函数是 URI 类的一部分。函数getAuthority() 返回指定 URI 的权限。 URL 的授权部分是 URI 的主机名和端口。

函数签名

public String getAuthority()

语法

url.getAuthority()

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

返回类型函数返回字符串类型,即指定 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

]