The getAuthority()
method of the URL
class can be used to get the authority part of a URL
object. The authority part of a URL
object refers to the hostname with the port of the URL.
The
URL
class is present in thejava.net
package.
The syntax of the getAuthority()
method is as follows:
public String getAuthority()
This method doesn’t take any argument. The return type of the getAuthority()
method is String
.
Note: The
getAuthority()
method returns the hostname with the port of the URL, whereas thegetHost()
method only returns the hostname.
The code below demonstrates how to use the getAuthority()
method:
import java.net.URL;class GetAuthorityExample {public static void main( String args[] ) {try {URL url= new URL("https:// www.educative.io:8000");String authority = url.getAuthority();System.out.println("The URL is : "+url);System.out.println("Authority is : "+ authority);} catch (Exception e) {System.out.println(e);}}}
In the code above:
URL
object.URL url= new URL("https:// www.educative.io:8000");
getAuthority()
method to get the authority part of the URL
object.String authority =url.getAuthority();
URL
and the authority
of the URL
.