Java 中的 URL getAuthority()方法,带示例
原文:https://www . geesforgeks . org/URL-getauthority-method-in-Java-with-examples/
getAuthority() 函数是 URL 类的一部分。函数 getAuthority()返回指定网址的权限。网址的授权部分是主机名和网址的端口。
功能签名
public String getAuthority()
语法
url.getAuthority()
参数:该功能不需要任何参数
返回类型:函数返回字符串类型
下面程序说明了 getHost()函数的使用:
例 1:
// Java program to show the
// use of the function getAuthority()
import java.net.*;
class Solution {
public static void main(String args[])
{
// url object
URL url = null;
try {
// create a URL
url
= new URL("https:// www.geeksforgeeks.org");
// get the Authority
String authority
= url.getAuthority();
// display the URL
System.out.println("URL = "
+ url);
// display the Authority
System.out.println("Authority = "
+ authority);
}
// if any error occurs
catch (Exception e) {
// display the error
System.out.println(e);
}
}
}
Output:
URL = 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 Solution {
public static void main(String args[])
{
// url object
URL url = null;
try {
// create a URL
url
= new URL(
"https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/");
// get the Authority
String authority
= url.getAuthority();
// get the Host
String host = url.getHost();
// display the URL
System.out.println("URL = " + url);
// 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);
}
}
}
Output:
URL = https:// www.geeksforgeeks.org:80/url-samefile-method-in-java-with-examples/
Authority = www.geeksforgeeks.org:80
Host = www.geeksforgeeks.org
版权属于:月萌API www.moonapi.com,转载请注明出处