The getAuthority() function is a part of URL class. The function getAuthority() returns the authority of a specified URL. The Authority part of the URL is the host name and the port of the URL.
Function Signature
public String getAuthority()
Syntax
url.getAuthority()
Parameter: This function does not require any parameter
Return Type: The function returns String Type
Below programs illustrates the use of getHost() function:
Example 1:
import java.net.*;
class Solution {
public static void main(String args[])
{
URL url = null ;
try {
url
= new URL( "https:// www.geeksforgeeks.org" );
String authority
= url.getAuthority();
System.out.println( "URL = "
+ url);
System.out.println( "Authority = "
+ authority);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
URL = https:// www.geeksforgeeks.org
Authority = www.geeksforgeeks.org
Example 2: The difference between getAuthority() and getHost() function is that getAuthority() returns the host along with the port but getHost() returns only the host name.
import java.net.*;
class Solution {
public static void main(String args[])
{
URL url = null ;
try {
url
= new URL(
"https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/" );
String authority
= url.getAuthority();
String host = url.getHost();
System.out.println( "URL = " + url);
System.out.println( "Authority = "
+ authority);
System.out.println( "Host = " + host);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
URL = https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/
Authority = www.geeksforgeeks.org:80
Host = www.geeksforgeeks.org